Jump to content

[SOLVED] str_replace alternate blank space


vinpkl

Recommended Posts

hi all

 

i can replace every " " blank space with "," str_replace

 

$pn=$row_ot['product_name'];
$pn = str_replace(" ", ",", $pn); 

 

but if i want to replace every "alternate blank space" then what will be the solution.

 

Means i want to leave first blank space as it is and would like to replace the second blank space with ",".

 

vineet

hi all

 

i can replace every " " blank space with "," str_replace

 

$pn=$row_ot['product_name'];
$pn = str_replace(" ", ",", $pn); 

 

but if i want to replace every "alternate blank space" then what will be the solution.

 

Means i want to leave first blank space as it is and would like to replace the second blank space with ",".

 

vineet

 

Can you do something as simple as this?

$pn=$row_ot['product_name'];
$pn = str_replace("  ", " ,", $pn); 

Being SPACE SPACE / SPACE COMMA. That should work fine as long as it's consecutive, unless you mean every second occurance, in that case regex I'm not sure about.

hi oni-kun

 

may be my question was not clear. so here is explanation.

 

like the string is "sony ericsson w995 black".

 

so here with second blank space i mean the blank space after sony ericsson.

 

with your code

$pn=$row_ot['product_name'];
$pn = str_replace("  ", " ,", $pn); 

 

it will search for two blank spaces together.

 

vineet

hi avvllvva

 

i used your code but i didnt understand

 

<?php
$new_str ='';
$pn="sony ericsson w995 black";
$array_splitted = explode(" ", $pn);
for($k=1; $k<=sizeof($array_splitted);$k++){
  $new_str .= $array_splitted[$k];
}
$final_str  = $array_splitted[0]." ".$new_str;
echo $final_str;
?>

 

it outputs


sony ericssonw995black 

 

but i want to output


sony ericsson, w995 black 

 

vineet

Oops, I didn't understood ur requirement well.

 

Okay now try this code

<?php
$new_str ='';
$pn= $row_ot['product_name'];
$array_splitted = explode(" ", $pn);
for($k=0; $k<=sizeof($array_splitted);$k++){
  if($k==1)
    $separator = ", ";
  else
    $separator = " ";
  $new_str .= $array_splitted[$k].$separator;
}
$final_str = $new_str;
?>

I suck at regular expressions, so I'm sure this is not the most efficient, but it beats the hell out of splitting the text into an array:

 

$row_ot['product_name']="sony ericsson w995 black";

$pn = preg_replace('/([^ ]*?)( )([^ ]*?)( )/', "\1 \3,", $row_ot['product_name']);

echo $pn;

//Output: "sony ericsson,w995 black"

 

This works no matter how many words are in the string. Although it wouldn't handle consecutive spaces well.

 

Edit (improved version):

$row_ot['product_name']="   sony   ericsson    w995    black";

$pn = preg_replace('/([^ ]+?)( +)([^ ]+?)( +)/', "\\1\\2\\3, ", $row_ot['product_name']);

echo $pn;

//Output: "   sony   ericsson, w995    black"

Oops, I didn't understood ur requirement well.

 

Okay now try this code

<?php
$new_str ='';
$pn= $row_ot['product_name'];
$array_splitted = explode(" ", $pn);
for($k=0; $k<=sizeof($array_splitted);$k++){
  if($k==1)
    $separator = ", ";
  else
    $separator = " ";
  $new_str .= $array_splitted[$k].$separator;
}
$final_str = $new_str;
?>

 

hi avvllvva

 

it works fine now but it works for only once. means it replaces only one time.

 

if the string is

$pn="sony ericsson w995 black"

then it will output

sony ericsson, w995 black 

 

that is fine but if the string is

$pn="sony ericsson w995 black new model just get"

 

then also i get only one comma

sony ericsson, w995 black new model just get 

 

is it possible to replace every alternate blank space with ","

 

vineet

I think mjdamato's way of using regex is better to this.

 

If u dont want regex , here is the updated for loop

 

for($k=0; $k<=sizeof($array_splitted);$k++){
  $rr = $k%2;
  if($rr==1 && ( $k<= ( sizeof($array_splitted)-2) ))
    $separator = ", ";
  else
    $separator = " ";
  $new_str .= $array_splitted[$k].$separator;
}

hi avvllvva

 

thanks for the solution. it works great.

 

vineet

 

I think mjdamato's way of using regex is better to this.

 

If u dont want regex , here is the updated for loop

 

for($k=0; $k<=sizeof($array_splitted);$k++){
  $rr = $k%2;
  if($rr==1 && ( $k<= ( sizeof($array_splitted)-2) ))
    $separator = ", ";
  else
    $separator = " ";
  $new_str .= $array_splitted[$k].$separator;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.