RE: [PHP] ucwords Except 'The' 'And' etc?
| From: | Steve Edberg | Date: | Tue, 14 Aug 2001 11:51:24 +0000 |
| Subject: | RE: [PHP] ucwords Except 'The' 'And' etc? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-62618@lists.php.net to get a copy of this message | ||
I know, I know, I have a lot of work to do...I shouldn't be doing this. I did get to play with PCRE regexps here, though...lotsa cool features over eregs!. Anyway, here's a *** NEW!!! IMPROVED!!! *** version of smart_ucwords(), that's probably way too much overkill. I did some quick tests, seems to work. Beware of email-induced line-wrapping, particularly in the comment black & quoted strings:
<?php
function smart_ucwords($String, $ForceCase=1, $CapitalizeTrailing=1) {
################################################################################
#
# A more sophisticated approach to title-casing.
#
## PARAMETERS ##################################################################
#
# $String string Input string. # $ForceCase 0|1 If true, will force title case (only first letter # capitalized, no matter what case the string is in. # $CaptializeTrailing 0|1 If true, will capitalize a normally-excepted # word if it is the last word in the string. So, a # string like 'Shot In the Dark, A' works properly.# ## USAGE NOTES ################################################################# # # The Exception List words should be in the case you desire; normally this will # be lowercase. # # The Invariant List contains words and titles (eg; NASA, MySQL, PostGreSQL, # eBusiness) whose capitalization should be fixed, no matter what. # # If one of these arrays is not to be used, simply set it to array(). # ## MODIFICATION HISTORY ######################################################## # # 14 august 2001: S. Edberg, UCDavis <sbedberg@ucdavis.edu> # Original # ################################################################################ $EXCEPTION_LIST = array(
'a', 'an', 'and', 'at', 'but', 'for', 'nor', 'or', 'some', 'the', 'to', 'with'); $INVARIANT_LIST = array(
'NASA', 'MySQL', 'PostGreSQL', 'eBusiness'); # First, strip & save leading & trailing whitespace preg_match(
'/^(\s*?)(.*)(\s*)$/U',
($ForceCase ? strtolower($String) : $String),
$StringParts
);
# Replace exception words
$String = ucwords($StringParts[2].($CapitalizeTrailing ? '' : ' '));
foreach ($EXCEPTION_LIST as $Word) {
$String = preg_replace("/(\s+)$Word(\s+)/i", "\\1$Word\\2", $String);
}
# Replace invariants
$String = " $String ";
foreach ($INVARIANT_LIST as $Word) {
$String = preg_replace("/(\s+)$Word(\s+)/i", "\\1$Word\\2", $String);
}
# Replace whitespace and return
return $StringParts[1].trim($String).$StringParts[3];
}
?>
At 4:09 PM -0500 8/13/01, Jeff Oien wrote:
Fabulous. Thanks to Steve and Mark. Exactly what I needed. Jeff Oien Something like this, perhaps (untested):-- +------ 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 ---+I needed this too so I just tested it.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); }this line should be more like: $String = eregi_replace("([[:space:]]+)".$Word."([[:space:]]+)", "\\1".$Word."\\2", $String);return $String; # LINE B }thanks! - Mark