sangoku Posted April 8, 2010 Share Posted April 8, 2010 Hy i have a string like SomeThing ... usually i use explode when i do it... but how can i make this thing to explode??? except that i go trough the string char for char and compare if it is upper or lower case...QQ Quote Link to comment https://forums.phpfreaks.com/topic/197945-how-can-i-divide-thisstring/ Share on other sites More sharing options...
premiso Posted April 8, 2010 Share Posted April 8, 2010 str_split will convert a string to an array of the characters in it. Quote Link to comment https://forums.phpfreaks.com/topic/197945-how-can-i-divide-thisstring/#findComment-1038685 Share on other sites More sharing options...
sangoku Posted April 8, 2010 Author Share Posted April 8, 2010 Ye right make a hell of a server load to.... i can fetch a string to.... before arrays there were strings ... what was before string... int? :-\ preg_filter — Perform a regular expression search and replace preg_grep — Return array entries that match the pattern preg_last_error — Returns the error code of the last PCRE regex execution preg_match_all — Perform a global regular expression match preg_match — Perform a regular expression match preg_quote — Quote regular expression characters preg_replace_callback — Perform a regular expression search and replace using a callback preg_replace — Perform a regular expression search and replace preg_split — Split string by a regular expression which one and how to use it??? i think it is sine of thouse ,,, just have to figure how to use it.... Quote Link to comment https://forums.phpfreaks.com/topic/197945-how-can-i-divide-thisstring/#findComment-1038688 Share on other sites More sharing options...
Zane Posted April 8, 2010 Share Posted April 8, 2010 premiso was trying to steer you right with str_split.. it's afterwards that you'd use regex.. (the preg functions) $aWord = str_split("SomeThing"); foreach($aWord as $letter) echo preg_match("#[A-Z]#", $letter) ? "It's uppercase!" : "It's lowercase!"; Quote Link to comment https://forums.phpfreaks.com/topic/197945-how-can-i-divide-thisstring/#findComment-1038714 Share on other sites More sharing options...
teamatomic Posted April 8, 2010 Share Posted April 8, 2010 preg_replace is the way to go $str = 'worksForAllButSingleCaps'; $newstr = preg_replace('/(.)([A-Z])/','\\1 \\2', $str); echo $newstr; $words=explode(" ",$newstr); echo "<pre>"; print_r($words); HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/197945-how-can-i-divide-thisstring/#findComment-1038784 Share on other sites More sharing options...
five Posted April 8, 2010 Share Posted April 8, 2010 i wrote this function breakAt(array $positions, $str) { $prev = 0; foreach ($positions as $pos) { $arr[] = substr($str, $prev, $pos-$prev); $prev = $pos; } $arr[] = substr($str, $pos); return $arr; } function str_splitByPreg($pattern, $str) { foreach (str_split($str,1) as $pos => $char) if (preg_match($pattern, $char)>0) $brkPos[$pos] = $pos; return (empty($brkPos)) ? array($str) : breakAt($brkPos, $str); } function str_splitBy($byFunc, $str) { foreach (str_split($str,1) as $pos => $char) if ($byFunc($char)) $brkPos[$pos] = $pos; return (empty($brkPos)) ? array($str) : breakAt($brkPos, $str); } function isLowerCase($char) { if ( (ord($char) > 96) && (ord($char) < 123) ) return TRUE; return FALSE; } print_r(str_splitBy('isLowerCase', $str)); print_r(str_splitByPreg('/[a-z]/', $str)); Quote Link to comment https://forums.phpfreaks.com/topic/197945-how-can-i-divide-thisstring/#findComment-1038909 Share on other sites More sharing options...
oni-kun Posted April 8, 2010 Share Posted April 8, 2010 Those should suffice nicely to your task in hand. If you're worring about the server load it takes to split a string, Think roughly 172+92 bytes for **ZVALs on ZEND's engine + C's overhead, And in today's memory? It wouldn't take much load at all on anything under 1M+ len. EDIT: Salathe, back off :-\ Quote Link to comment https://forums.phpfreaks.com/topic/197945-how-can-i-divide-thisstring/#findComment-1038915 Share on other sites More sharing options...
salathe Posted April 8, 2010 Share Posted April 8, 2010 sangoku, can you describe more thoroughly what you want? The details are important for offering a solution. If the aim is to "explode" into an array, you could use one way; to "explode" into a space-separated string you would use a different way. So, please describe as best you can what you want to get from the string. [ot]Edit: I hadn't even posted before oni-kun edited with "Salathe, back off :-\" I wasn't going to comment on oni-kun's post anyway![/ot] Quote Link to comment https://forums.phpfreaks.com/topic/197945-how-can-i-divide-thisstring/#findComment-1038918 Share on other sites More sharing options...
sangoku Posted April 8, 2010 Author Share Posted April 8, 2010 X-D Ok i done it but it dous not do what i want it to found some function on other forum which works for me function humanize( $input ) { $input = preg_replace( '/([^A-Z])([A-Z][a-z]+)/U', '$1 $2', $input ); $input = preg_split( '/\s+/', $input, -1, PREG_SPLIT_NO_EMPTY ); $input = implode( ' ', $input ); // $input = strtolower( $input ); return $input; } $string = 'Test testTest TesT.test test/test [test]test test(test)'; $string = humanize( $string ); echo $string; //if should output : Test testTest TesT test test test [test] test test (test) //but it outputs Test testTest TesT.test test/test test[test] test test(test) Ok how can i bypass the est/test test[test] test test(test) part the . is not important i take it out before this XD Quote Link to comment https://forums.phpfreaks.com/topic/197945-how-can-i-divide-thisstring/#findComment-1038951 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.