RE: [PHP] ucwords Except 'The' 'And' etc?
| From: | Steve Edberg | Date: | Mon, 13 Aug 2001 18:53:52 +0000 |
| Subject: | RE: [PHP] ucwords Except 'The' 'And' etc? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-62477@lists.php.net to get a copy of this message | ||
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 vanguard.
-steve
At 11:07 AM -0500 8/13/01, Boget, Chris <Chris.Boget@wild.net> wrote:
Is there a prewritten function for capitalizing the first-- +------ Factoid: Of the 100 largest economies in the world, 51 are ------+ | Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ | +--- corporations ------ http://www.ips-dc.org/reports/top200text.htm ---+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 LeaderNone 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... Chris