robert_gsfame Posted August 8, 2009 Share Posted August 8, 2009 hello codemasters... i would like to ask some simple question, how can i change every first letter into capital letter for example : <?php $name = 'JIM HUMPHREY';?> and i would like to make it into this when i call $name using echo Jim Humphrey and how bout if they have 3 words separated with space for example : $name = JIM HUMPHREY GODFREY and it should result Jim Humphrey Godfrey thanks in advance Link to comment https://forums.phpfreaks.com/topic/169361-solved-manipulate-string/ Share on other sites More sharing options...
smerny Posted August 8, 2009 Share Posted August 8, 2009 echo ucwords($name); Link to comment https://forums.phpfreaks.com/topic/169361-solved-manipulate-string/#findComment-893639 Share on other sites More sharing options...
robert_gsfame Posted August 8, 2009 Author Share Posted August 8, 2009 No it doesn't work if all letters are capital letter.. if $name = JAMES HUGHES GODFREY then if i use ucwords then it will result the same unless i have created $name = james hughes godfrey So does it mean i have to create $name2 = strtolower($name) then ucwords($name2)???? it will need more effort, or do you have any better idea? Link to comment https://forums.phpfreaks.com/topic/169361-solved-manipulate-string/#findComment-893646 Share on other sites More sharing options...
smerny Posted August 8, 2009 Share Posted August 8, 2009 ucwords(strtolower($name)) Link to comment https://forums.phpfreaks.com/topic/169361-solved-manipulate-string/#findComment-893647 Share on other sites More sharing options...
ingeva Posted August 8, 2009 Share Posted August 8, 2009 ucwords(strtolower($name)) I prefer to use: function titlecase($name) { return mb_convert_case(trim($name),MB_CASE_TITLE); } In order to use it you MAY have to define the character set first: $charset = "utf-8"; mb_internal_encoding ($charset); I have similar functions for uppercase and lowercase. With the local functions you don't have to remember the MB_CASE_.... constants. Link to comment https://forums.phpfreaks.com/topic/169361-solved-manipulate-string/#findComment-893675 Share on other sites More sharing options...
robert_gsfame Posted August 9, 2009 Author Share Posted August 9, 2009 any other ideas? i have this problem with strtolower(ucwords what if the strings that i want to manipulate is job title for example : $name = FINANCE MANAGER (FM) if i use echo strtolower(ucwords($name)) then it comes Finance Manager (fm) it should be --------------> Finance Manager (FM) any other ideas? Link to comment https://forums.phpfreaks.com/topic/169361-solved-manipulate-string/#findComment-893903 Share on other sites More sharing options...
Alex Posted August 9, 2009 Share Posted August 9, 2009 any other ideas? i have this problem with strtolower(ucwords what if the strings that i want to manipulate is job title for example : $name = FINANCE MANAGER (FM) if i use echo strtolower(ucwords($name)) then it comes Finance Manager (fm) it should be --------------> Finance Manager (FM) any other ideas? Will the things that you want non-effected be within ( )? If so you can do this: <?php $name = 'FINANCE MANAGER (FM)'; $split = explode(' ', $name); foreach($split as &$part) { $part = ($part{0} != '(') ? ucfirst(strtolower($part)) : $part; } $name = implode(' ', $split); echo $name; Output: Finance Manager (FM) Link to comment https://forums.phpfreaks.com/topic/169361-solved-manipulate-string/#findComment-893908 Share on other sites More sharing options...
The Little Guy Posted August 9, 2009 Share Posted August 9, 2009 Here is what I came up with: <?php $str = ucwords(strtolower('FINANCE MANAGER (FM)')); if(preg_match("~\((.+?)\)~i",$str,$matches)){ $match = strtoupper($matches[0]); $str = preg_replace("~\((.+?)\)~i", $match, $str); } echo $str; ?> Link to comment https://forums.phpfreaks.com/topic/169361-solved-manipulate-string/#findComment-893921 Share on other sites More sharing options...
robert_gsfame Posted August 9, 2009 Author Share Posted August 9, 2009 COOL ANSWER...!!! i thought there is a command to do this...THANX GUYS Link to comment https://forums.phpfreaks.com/topic/169361-solved-manipulate-string/#findComment-894020 Share on other sites More sharing options...
nrg_alpha Posted August 9, 2009 Share Posted August 9, 2009 Here is what I came up with: <?php $str = ucwords(strtolower('FINANCE MANAGER (FM)')); if(preg_match("~\((.+?)\)~i",$str,$matches)){ $match = strtoupper($matches[0]); $str = preg_replace("~\((.+?)\)~i", $match, $str); } echo $str; ?> Alternatively, you could do away with the need to create $match by integrating the $matches[1] right into an str_replace instead: $str = ucwords(strtolower('FINANCE MANAGER (FM)')); $str = (preg_match('#\(([^)]+)\)#', $str, $matches))? str_replace($matches[1], strtoupper($matches[1]), $str): $str; echo $str; Link to comment https://forums.phpfreaks.com/topic/169361-solved-manipulate-string/#findComment-894172 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.