[solved] Getting a string up to a specified string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

[solved] Getting a string up to a specified string

Post by dallasx »

I have strings with company names... Every webmaster wants to control and correct the data from the mistakes of users.
Given company names I have want to strip the Inc. off the very end and save everything up to the Inc. After I do that, I'll add my own Inc.

The common mistypings of Inc. are "inc" "inc." "INC" etc, etc...

Code: Select all

$string = "THOMAS BROS INC.";
How would I take off the INC?

My method isn't working.
Last edited by dallasx on Thu Nov 10, 2005 3:15 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

substr() would be fastest if you know inc. is the last set of characters, otherwise, regex is probably the most flexible..

Code: Select all

$string = preg_replace('#\s*inc\.\s*$#i','',$string);
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Post by dallasx »

I've seen that before but what do all the slashes and asterisks mean?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

read the stickies in the regex board..
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Post by dallasx »

Code: Select all

$string = "THOMAS BROS. INC.";
    $front = preg_replace('#\s*inc\.\s*$#i','',$string);
    $parts = explode(" ",$front);
    $c = 0;
    while($c <= count($parts))
    {
        $tmp = $tmp." ".ucfirst(strtolower($parts[$c]));
        $c++;
    }
    $tmp = $tmp.", Inc.";
    echo "<hr>$tmp<hr>";

    // OUTPUT: Thomas Bros. Inc.
I cut my number of lines in half.

Thanks feyd!
Post Reply