Page 1 of 1

[solved] Getting a string up to a specified string

Posted: Tue Nov 08, 2005 10:48 am
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.

Posted: Tue Nov 08, 2005 10:52 am
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);

Posted: Tue Nov 08, 2005 11:05 am
by dallasx
I've seen that before but what do all the slashes and asterisks mean?

Posted: Tue Nov 08, 2005 11:10 am
by feyd
read the stickies in the regex board..

Posted: Tue Nov 08, 2005 11:11 am
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!