Jump to content

Replace whitespaces with a comma only for the first few occurances.


Recommended Posts

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

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.

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

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

 

 

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.

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;
?>

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.

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 ?

 

 

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
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.