RE: [PHP] ucwords Except 'The' 'And' etc?
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 [EMAIL PROTECTED] # 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): 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 -- +-- Factoid: Of the 100 largest economies in the world, 51 are --+ | Steve Edberg University of California, Davis | | [EMAIL PROTECTED] Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ | +--- corporations -- http://www.ips-dc.org/reports/top200text.htm ---+ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] ucwords Except 'The' 'And' etc?
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 Thanks. Jeff Oien -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] ucwords Except 'The' 'And' etc?
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... Chris
Re: [PHP] ucwords Except 'The' 'And' etc?
http://www.php.net/manual/en/function.ucwords.php You can read all about the limit of this function, but this is the closest thing to what you're looking for. Rick 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 Thanks. Jeff Oien -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] ucwords Except 'The' 'And' etc?
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 [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... Chris -- +-- Factoid: Of the 100 largest economies in the world, 51 are --+ | Steve Edberg University of California, Davis | | [EMAIL PROTECTED] Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ | +--- corporations -- http://www.ips-dc.org/reports/top200text.htm ---+ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] ucwords Except 'The' 'And' etc?
Something like this, perhaps (untested): 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] ucwords Except 'The' 'And' etc?
Fabulous. Thanks to Steve and Mark. Exactly what I needed. Jeff Oien Something like this, perhaps (untested): 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]