receiver19 Posted June 1, 2009 Share Posted June 1, 2009 Let's say I have a string "GetToKnowYou". I wish to search the upper capital of that string and separate them into "Get To Know You" Any idea? Thanks in advanced. Quote Link to comment Share on other sites More sharing options...
BobcatM Posted June 1, 2009 Share Posted June 1, 2009 <?php $string = $_GET['string']; $count = strlen($string); $i = 0; $ii = 0; while($i < $count) { $char = $string{$i}; if(ereg("[A-Z]", $char, $val)){ $ii++; $strings[$ii] .= $char; } else { $strings[$ii] .= $char; } $i++; } var_dump($strings); ?> Quote Link to comment Share on other sites More sharing options...
BobcatM Posted June 1, 2009 Share Posted June 1, 2009 <? $str="GetToKnowYou"; preg_match_all('/[A-Z][^A-Z]*/',$str,$results); ?> Or an easier way. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 1, 2009 Share Posted June 1, 2009 <?php $str = 'GetToKnowYou'; $new_str = rtrim(preg_replace('#([A-Z][^A-Z]*)#', '$1 ', $str)); var_dump($new_str); Quote Link to comment Share on other sites More sharing options...
receiver19 Posted June 1, 2009 Author Share Posted June 1, 2009 Thanks for all the replied. <?php $str = 'GetToKnowYou'; $new_str = rtrim(preg_replace('#([A-Z][^A-Z]*)#', '$1 ', $str)); var_dump($new_str); This works very well! However, i don't get it on this part ('#([A-Z][^A-Z]*)#', '$1 ', . I still don't understand though i have read the php manual. Anyone mind to explain to me? Quote Link to comment Share on other sites More sharing options...
receiver19 Posted June 1, 2009 Author Share Posted June 1, 2009 <? $str="GetToKnowYou"; preg_match_all('/[A-Z][^A-Z]*/',$str,$results); ?> Or an easier way. BobcatM, i have tried this one. However, when i echo it out. The results isn't right, any idea on it? Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted June 1, 2009 Share Posted June 1, 2009 The preg_replace() function searches through a string matching the whole thing or parts of it to a regular expression pattern. In this case the the pattern '#([A-Z][^A-Z]*)#' means match a string starting with one capital letter followed by any number of characters not matching a capital letter. #: a delimiter marking the start of the actual regular expression pattern (: start of a capture group [A-Z]: a character class matching one character in the range A-Z [^A-Z]*: a character class matching one character not in the range A-Z (that could be a, b, c, 12, #, [ ect.) and actually any number of them (including zero) because of the modifier * ): end of capture group #: a delimiter marking the end of the actual regular expression pattern Now the syntax of the preg_replace() is: preg_replace($regular_expression, $what_to_replace_with, $string_to_search_and_replace). The search is done with the previous mentioned pattern and we are using '$1 ' as the replacement. $1 means the first capture group or sub pattern - what's inside the parentheses - it's called a back reference. So each time the regex engine matches the previously discussed pattern it assign it's value to $1 and '$1 ' of course adds a space. So going through this string 'GetToKnowYou' will first match 'Get' ([A-Z] followed by two [^A-Z]) and assign it's value to $1. So 'Get' is replaced with 'Get '. Now it matches 'To' and replaces it with 'To ' and so on. So in the end it's 'Get To Know You'. Read more on regular expression here: http://www.regular-expressions.info/, http://www.regular-expressions.info/tutorial.html, http://www.regular-expressions.info/reference.html Quote Link to comment Share on other sites More sharing options...
receiver19 Posted June 2, 2009 Author Share Posted June 2, 2009 Very well explanation. I have clearer understanding. Thanks a lot!! And, thanks to everyone reply to post. I really appreciate your all kindly help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.