Something like this, perhaps (untested):

function smart_ucwords($String)

        $ExceptionList = array('the', 'an', 'a'); # should all be in lowercase

        $String = ucwords(strtolower(ltrim($String))); # LINE A

        foreach ($ExceptionList as $Word)
        $String = eregi_replace("[[:space:]]+$Word[[:space:]]+", 
$Word, $String);

        return $String; # LINE B


This will leave the exception word capitalized if it is the first 
word in the $String (as it should be), or if it is the last work in 
the $String (unlikely). If you want to handle cases where the 
exception word comes at the end of the string, just replace LINE A 
above with

        $String = ucwords(strtolower($String)).' ';

and LINE B with

        return rtrim($String);

Of course, there will be exceptions that are near-impossible to 
programmatically determine, eg;
        'Dr. Martin van Hooten Hears a Who'

in this case, van should be lower case, since it isn't referring to a 
medium-capacity box-like automobile, tennis shoe brand, or naval 


At 11:07 AM -0500 8/13/01, Boget, Chris <[EMAIL PROTECTED]> wrote:
>  > Is there a prewritten function for capitalizing the first
>>  letter of each word in a string except for the common
>>  words you wouldn't want to capitalize in a title? Like
>>  Come Learn the Facts From an Industry Leader
>None that I've seen.
>But it wouldn't be hard to write a function that takes a
>string, passes that string to ucwords() and then you can
>run through a pre-defined array and just do an ereg_replace()
>on those words you want to remain lowercase...
>Or something like that...

+------ Factoid: Of the 100 largest economies in the world, 51 are ------+
| Steve Edberg                           University of California, Davis |
| [EMAIL PROTECTED]                               Computer Consultant |
|         |
+--- corporations ------ ---+

PHP General Mailing List (
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to