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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.