note 84920 added to function.ucwords
| From: | majd87 at gmail dot com | Date: | Wed, 06 Aug 2008 01:58:25 +0000 |
| Subject: | note 84920 added to function.ucwords | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143195@lists.php.net to get a copy of this message | ||
I modified Q1712's code (below) to use regular expressions
instead of characters to properly capitalize words that fall
directly after an unwanted character. See his post for details.
This version allows me to use it around html elements, etc.
function my_ucwords($string){
$invalid_characters = array('"',
'\(',
'\[',
'\/',
'<.*?>',
'<\/.*?>');
foreach($invalid_characters as $regex){
$string = preg_replace('/('.$regex.')/','$1 ',$string);
}
$string=ucwords($string);
foreach($invalid_characters as $regex){
$string = preg_replace('/('.$regex.') /','$1',$string);
}
return $string;
}
Moreover, to get a proper title case, i combine it with this function:
//This Function is obtained from:
http://codesnippets.joyent.com/posts/show/716
function title_case($title) {
$smallwordsarray = array(
'of','a','the','and','an','or','nor','but','is','if','then',
'else','when',
'at','from','by','on','off','for','in','out',
'over','to','into','with'
);
$words = explode(' ', $title);
foreach ($words as $key => $word)
{
if ($key == 0 or !in_array($word, $smallwordsarray))
$words[$key] = $this->my_ucwords(strtolower($word));
}
$newtitle = implode(' ', $words);
return $newtitle;
}
Hope you find it useful.
----
Server IP: 208.69.120.35
Probable Submitter: 71.65.20.84
----
Manual Page -- http://www.php.net/manual/en/function.ucwords.php
Edit -- https://master.php.net/note/edit/84920
Del: integrated -- https://master.php.net/note/delete/84920/integrated
Del: useless -- https://master.php.net/note/delete/84920/useless
Del: bad code -- https://master.php.net/note/delete/84920/bad+code
Del: spam -- https://master.php.net/note/delete/84920/spam
Del: non-english -- https://master.php.net/note/delete/84920/non-english
Del: in docs -- https://master.php.net/note/delete/84920/in+docs
Del: other reasons-- https://master.php.net/note/delete/84920
Reject -- https://master.php.net/note/reject/84920
Search -- https://master.php.net/manage/user-notes.php