l_kris06 Posted March 8, 2008 Share Posted March 8, 2008 Hi all, I need to replace whitespaces with a comma only for the first few occurances. For example: $a = "1 A AB 2 ABC 3 ABCD 4 5 The whitespace inbetween these must not be replaced with a comma"; I need to replace the spaces between "1" and "The" with a comma(,) . The remaining part of the string must be left untouched. This resultant string must go into a variable. desired output: -------------- 1,A,AB,2,ABC,3,ABCD,4,5,The whitespace inbetween these must not be replaced with a comma I would really appreciate, if somebody can help me out. Thanks. /Kris Quote Link to comment https://forums.phpfreaks.com/topic/95012-replace-whitespaces-with-a-comma-only-for-the-first-few-occurances/ Share on other sites More sharing options...
Stooney Posted March 8, 2008 Share Posted March 8, 2008 Is the length of the string static? (does it stay the same). If so you could just replace it using the string offset. ex $string="A B C D"; echo $string[2]; //outputs B You could use this to replace the commas. But that's only if it's a static length. Otherwise you might need to look into regex. Quote Link to comment https://forums.phpfreaks.com/topic/95012-replace-whitespaces-with-a-comma-only-for-the-first-few-occurances/#findComment-486688 Share on other sites More sharing options...
l_kris06 Posted March 8, 2008 Author Share Posted March 8, 2008 The length of string is not static. I am stuck. any help ? Quote Link to comment https://forums.phpfreaks.com/topic/95012-replace-whitespaces-with-a-comma-only-for-the-first-few-occurances/#findComment-486693 Share on other sites More sharing options...
Stooney Posted March 8, 2008 Share Posted March 8, 2008 You would need preg_replace(). Read up on it here: http://us.php.net/preg_replace You can choose the pattern to replace, and the max number of replacements. Quote Link to comment https://forums.phpfreaks.com/topic/95012-replace-whitespaces-with-a-comma-only-for-the-first-few-occurances/#findComment-486695 Share on other sites More sharing options...
laffin Posted March 8, 2008 Share Posted March 8, 2008 Looks like the common denominator is that the Whitespaces are between CAPS ALPHANUMS. as stated preg_replace is what ya looking for but since we know the pattern it makes it very simple <?php $str='1 A AB 2 ABC 3 ABCD 4 5 The whitespace inbetween these must not be replaced with a comma'; $rpl=preg_replace('@([A-Z0-9]+)( )@','$1,',$str); echo "$str<br>$rpl"; ?> // Output: //1 A AB 2 ABC 3 ABCD 4 5 The whitespace inbetween these must not be replaced with a comma //1,A,AB,2,ABC,3,ABCD,4,5,The whitespace inbetween these must not be replaced with a comma Taa-Daa Quote Link to comment https://forums.phpfreaks.com/topic/95012-replace-whitespaces-with-a-comma-only-for-the-first-few-occurances/#findComment-486703 Share on other sites More sharing options...
l_kris06 Posted March 8, 2008 Author Share Posted March 8, 2008 Hi Laffin, Thanks for the piece of code. It works like a charm for the example i had given, however in a real scenario it replaces all occurances of whitespaces with a comma. Heres the real data: $data = SIP25071 O 4.4 43 4 ERG 02/28/2008 Mike cisco2000 ptime.baseval header, incorrect chk in. after using the piece of code u suggested, i get SIP25071,O,4.4,43,4,ERG,02/28/2008,Mike cisco2000,ptime.baseval header, incorrect,chk,in. notice there is no comma between "Mike" and "cisco2000". my desired output is: -------------------- SIP25071,O,4.4,43,4,ERG,02/28/2008,Mike,cisco2000,ptime.baseval header incorrect chk in. These data are individual bug statistics. I eventually want to write to the database, hence the need for the delimiter. I would really appreciate, if somebody can fix this further. /Kris Quote Link to comment https://forums.phpfreaks.com/topic/95012-replace-whitespaces-with-a-comma-only-for-the-first-few-occurances/#findComment-486729 Share on other sites More sharing options...
laffin Posted March 8, 2008 Share Posted March 8, 2008 Thats a lot more complex, what u have to figure out is a pattern system. from the looks of it, u will probably have to create a phrase list, to be kept intact. and add commas everywhere except within a phrase list or look closely at yer output and look for any type of pattern between the values, or the values sequence. Quote Link to comment https://forums.phpfreaks.com/topic/95012-replace-whitespaces-with-a-comma-only-for-the-first-few-occurances/#findComment-486734 Share on other sites More sharing options...
sasa Posted March 8, 2008 Share Posted March 8, 2008 if you want to replace first 9 spaces with comma try <?php $a = '1 A AB 2 ABC 3 ABCD 4 5 The whitespace inbetween these must not be replaced with a comma'; //$a = 'SIP25071 O 4.4 43 4 ERG 02/28/2008 Mike cisco2000 ptime.baseval header incorrect chk in'; for ($c = 0;$c < 9; $c++) $a[strcspn($a,' ')] = ','; echo $a; ?> Quote Link to comment https://forums.phpfreaks.com/topic/95012-replace-whitespaces-with-a-comma-only-for-the-first-few-occurances/#findComment-486788 Share on other sites More sharing options...
corbin Posted March 8, 2008 Share Posted March 8, 2008 Expanding on laffins' idea, you could do: $str = preg_replace('/([A-Z]) ([A-Z])/', '$1,$2', $str); This assumes that the str you want to alter will never have a word ending in a capital followed by a word beginning with a capital in the section where you do not wish for spaces to go to commas. Does the date part (02/28/2008) stay at the same place? If so, you could just explode it, implode part a, implode part b and then put a and b together. Quote Link to comment https://forums.phpfreaks.com/topic/95012-replace-whitespaces-with-a-comma-only-for-the-first-few-occurances/#findComment-486790 Share on other sites More sharing options...
l_kris06 Posted March 8, 2008 Author Share Posted March 8, 2008 Given the time i have for dumping these results to a database, Sasa's idea seems quick enough, however the resultant output is message up. i get an output like this: ----------------------- SIP25071,,O,, 4.4,,,,, 43 4 ERG 02/28/2008 Mike cisco2000 ptime.baseval header incorrect chk in any help ? Quote Link to comment https://forums.phpfreaks.com/topic/95012-replace-whitespaces-with-a-comma-only-for-the-first-few-occurances/#findComment-486821 Share on other sites More sharing options...
laffin Posted March 8, 2008 Share Posted March 8, 2008 well if ya just need 9 commas <?php $str='SIP25071 O 4.4 43 4 ERG 02/28/2008 Mike cisco2000 ptime.baseval header incorrect chk in'; $rpl=preg_replace('@([^\s]+)(\s+)@','$1,',$str,9); echo "$str<br>$rpl"; //Output //SIP25071 O 4.4 43 4 ERG 02/28/2008 Mike cisco2000 ptime.baseval header incorrect chk in //SIP25071,O,4.4,43,4,ERG,02/28/2008,Mike,cisco2000,ptime.baseval header incorrect chk in ?> Quote Link to comment https://forums.phpfreaks.com/topic/95012-replace-whitespaces-with-a-comma-only-for-the-first-few-occurances/#findComment-486836 Share on other sites More sharing options...
l_kris06 Posted March 8, 2008 Author Share Posted March 8, 2008 Lafin, that did it. Thank you. Thanks all, for taking your time off to help me out, appreciate it so much. I got the solution exactly 94 mins before the ultimatum!! Thanks once again Lafin!! /Kris Quote Link to comment https://forums.phpfreaks.com/topic/95012-replace-whitespaces-with-a-comma-only-for-the-first-few-occurances/#findComment-486844 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.