vinpkl Posted August 11, 2009 Share Posted August 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/169715-solved-str_replace-alternate-blank-space/ Share on other sites More sharing options...
oni-kun Posted August 11, 2009 Share Posted August 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/169715-solved-str_replace-alternate-blank-space/#findComment-895357 Share on other sites More sharing options...
vinpkl Posted August 11, 2009 Author Share Posted August 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/169715-solved-str_replace-alternate-blank-space/#findComment-895363 Share on other sites More sharing options...
avvllvva Posted August 11, 2009 Share Posted August 11, 2009 <?php $new_str =''; $pn=$row_ot['product_name']; $array_splitted = explode(" ", $pn); for($k=1; $k<=sizeof($array_splitted);$k++){ $new_str .= $array_splitted[$k]; } $final_str = $array_splitted[0]." ".$new_str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/169715-solved-str_replace-alternate-blank-space/#findComment-895371 Share on other sites More sharing options...
vinpkl Posted August 11, 2009 Author Share Posted August 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/169715-solved-str_replace-alternate-blank-space/#findComment-895376 Share on other sites More sharing options...
avvllvva Posted August 11, 2009 Share Posted August 11, 2009 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/169715-solved-str_replace-alternate-blank-space/#findComment-895379 Share on other sites More sharing options...
Psycho Posted August 11, 2009 Share Posted August 11, 2009 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" Quote Link to comment https://forums.phpfreaks.com/topic/169715-solved-str_replace-alternate-blank-space/#findComment-895389 Share on other sites More sharing options...
vinpkl Posted August 11, 2009 Author Share Posted August 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/169715-solved-str_replace-alternate-blank-space/#findComment-895390 Share on other sites More sharing options...
avvllvva Posted August 11, 2009 Share Posted August 11, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/169715-solved-str_replace-alternate-blank-space/#findComment-895416 Share on other sites More sharing options...
vinpkl Posted August 11, 2009 Author Share Posted August 11, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/169715-solved-str_replace-alternate-blank-space/#findComment-895419 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.