ddrake1984 Posted March 22, 2012 Share Posted March 22, 2012 I need to split a string, but in a very unusual way. $string1 = "HelloWorld" $string2 = "AlphaBravo" $string3 = "BreakMeUp" I need the following split at each upppercase letter for this output Hello World Alpha Bravo Break Me Up Quote Link to comment https://forums.phpfreaks.com/topic/259469-split-a-string-or-can-you/ Share on other sites More sharing options...
rythemton Posted March 22, 2012 Share Posted March 22, 2012 $string = 'ThisText'; $newstring = ''; foreach( $string AS $letter ) { $newstring .= ( ctype_upper( $letter ) ? ' ' : '' ) . $letter; } $string = trim( $newstring ); Since this will put a space in front of every capital letter, I put the trim on the end to remove the space from the beginning. Quote Link to comment https://forums.phpfreaks.com/topic/259469-split-a-string-or-can-you/#findComment-1330079 Share on other sites More sharing options...
dragon_sa Posted March 22, 2012 Share Posted March 22, 2012 This will work <?php function splitByCaps($string){ return preg_replace('/([a-z0-9])?([A-Z])/','$1 $2',$string); } $text="HelloWorldOfDays"; echo splitByCaps($text); ?> Quote Link to comment https://forums.phpfreaks.com/topic/259469-split-a-string-or-can-you/#findComment-1330081 Share on other sites More sharing options...
ddrake1984 Posted March 22, 2012 Author Share Posted March 22, 2012 rythemton your script unfortunately didnt work. I have this error Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\index.php on line 52 dragon_sa yours worked but only one string worked. there was an error after the first string. Fatal error: Cannot redeclare splitByCaps() (previously declared in C:\xampp\htdocs\index.php:51) in C:\xampp\htdocs\index.php on line 51 my code is: while(odbc_fetch_row($sqlresult_products)) { echo "<tr>"; echo "<td><font size=1 face=verdana><b>"; $string = substr($phpresult_vendors,9,-9); function splitByCaps($string){ return preg_replace('/([a-z0-9])?([A-Z])/','$1 $2',$string); } echo splitByCaps($string); for the purpose of understanding the above script a bit better $phpresult_vendors are 'Products_IngramMicro_Products' 'Products_ExpressData_Products' 'Products_TheAmazingCompany_Products" etc. Quote Link to comment https://forums.phpfreaks.com/topic/259469-split-a-string-or-can-you/#findComment-1330086 Share on other sites More sharing options...
dragon_sa Posted March 22, 2012 Share Posted March 22, 2012 you cant have the function in the while loop do it like this function splitByCaps($string){ return preg_replace('/([a-z0-9])?([A-Z])/','$1 $2',$string); } while(odbc_fetch_row($sqlresult_products)) { echo "<tr>"; echo "<td><font size=1 face=verdana><b>"; $data = substr($phpresult_vendors,9,-9); echo splitByCaps($data); } EDIT: just echoed out the value instead of assigning to another variable to suit your script better Quote Link to comment https://forums.phpfreaks.com/topic/259469-split-a-string-or-can-you/#findComment-1330087 Share on other sites More sharing options...
rythemton Posted March 22, 2012 Share Posted March 22, 2012 Hmm. Since you can use array reference to check a letter, I thought it would work, but apparently it doesn't. This should work: $string = 'ThisText'; $newstring = ''; foreach( str_split( $string ) AS $letter ) { $newstring .= ( ctype_upper( $letter ) ? ' ' : '' ) . $letter; } $string = trim( $newstring ); Quote Link to comment https://forums.phpfreaks.com/topic/259469-split-a-string-or-can-you/#findComment-1330089 Share on other sites More sharing options...
Zane Posted March 22, 2012 Share Posted March 22, 2012 Personally, I think rythemton's method is better... Regardless, these forums aren't meant for code to be written for you only to jump back with the error. Research these errors. This isn't a race to see who can write the perfect code either. Just my 2 cents. Quote Link to comment https://forums.phpfreaks.com/topic/259469-split-a-string-or-can-you/#findComment-1330090 Share on other sites More sharing options...
dragon_sa Posted March 22, 2012 Share Posted March 22, 2012 I don't believe it was a race, if you look the code was posted only 30secs apart at the start, the OP had attempted to implement the code and we had both corrected the issues he had in our examples, I personally prefer a function over a loop but that is only my preference and I am not saying either way is better, they both reach the same result. Quote Link to comment https://forums.phpfreaks.com/topic/259469-split-a-string-or-can-you/#findComment-1330091 Share on other sites More sharing options...
ddrake1984 Posted March 22, 2012 Author Share Posted March 22, 2012 apologies Zane if I offended you, or rythemton and dragon_sa. I did not mean to use and abuse if that is what was seen. I only asked for some assistance with php and how to do a split in a string. I also don't expect this to be a race or method comparison. but I have tried to understand the errors and I am having difficulty. rythemton, your solution worked like a charm, I could kiss you. Quote Link to comment https://forums.phpfreaks.com/topic/259469-split-a-string-or-can-you/#findComment-1330094 Share on other sites More sharing options...
dragon_sa Posted March 22, 2012 Share Posted March 22, 2012 Certainly you can ask, I cant speak for the others but I am in no way offended and I do not mind which solution you use. Quote Link to comment https://forums.phpfreaks.com/topic/259469-split-a-string-or-can-you/#findComment-1330095 Share on other sites More sharing options...
salathe Posted March 22, 2012 Share Posted March 22, 2012 dragon_sa, your code inserts a space at the beginning of the string when the first character is uppercase. I would simplify the regular express to something like /\B(?=[A-Z])/ and make the replacement just a space character. Quote Link to comment https://forums.phpfreaks.com/topic/259469-split-a-string-or-can-you/#findComment-1330108 Share on other sites More sharing options...
dragon_sa Posted March 22, 2012 Share Posted March 22, 2012 Yes that does work better, nice pick up Quote Link to comment https://forums.phpfreaks.com/topic/259469-split-a-string-or-can-you/#findComment-1330112 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.