jeeva Posted June 25, 2008 Share Posted June 25, 2008 Hi frnds i'm having a problem with string replace. My problem is, i have a string like $str="php and and and perl"; now i have to make the string "and and and" in to "and". In order to do that i have following code.but it does not seems to work <?php $str="php and and and perl"; $replace=preg_replace("/( and ){2,20}+/"," and ",$query); echo $replace; ?> output it retruns the same "php and and and perl"; Please let me know wts wrong with my code.. Thanks Jeeva Quote Link to comment https://forums.phpfreaks.com/topic/111788-string-replace-problem/ Share on other sites More sharing options...
.josh Posted June 25, 2008 Share Posted June 25, 2008 well, I suck at regex, but here's my take: <?php $str="php and and and perl"; preg_match_all('/and/',$str,$count); $limit = count($count[0]) - 1; $replace=preg_replace("/and/","",$str,$limit); echo $replace; ?> Quote Link to comment https://forums.phpfreaks.com/topic/111788-string-replace-problem/#findComment-573889 Share on other sites More sharing options...
jeeva Posted June 25, 2008 Author Share Posted June 25, 2008 Thanks Crayon Violent, It works but when i try to replace with "and" it returns same output Modified code <?php $str="php and and and perl"; preg_match_all('/and/',$str,$count); $limit = count($count[0]) - 1; $replace=preg_replace("/and/","and",$str,$limit); echo $replace; ?> output remains same "php and and and perl" NOTE : one "and" should be there instead of more than one "and". Quote Link to comment https://forums.phpfreaks.com/topic/111788-string-replace-problem/#findComment-573896 Share on other sites More sharing options...
.josh Posted June 25, 2008 Share Posted June 25, 2008 umm...that's because you are replacing "and" with "and" lol. Quote Link to comment https://forums.phpfreaks.com/topic/111788-string-replace-problem/#findComment-573897 Share on other sites More sharing options...
.josh Posted June 25, 2008 Share Posted June 25, 2008 if you ran the code as I posted it, the output is: "php and perl" that is what you want, right? Quote Link to comment https://forums.phpfreaks.com/topic/111788-string-replace-problem/#findComment-573899 Share on other sites More sharing options...
jeeva Posted June 25, 2008 Author Share Posted June 25, 2008 No actually,the output is "php perl". there is no "and" between 2 words Quote Link to comment https://forums.phpfreaks.com/topic/111788-string-replace-problem/#findComment-573901 Share on other sites More sharing options...
.josh Posted June 25, 2008 Share Posted June 25, 2008 oh okay well then that's not how I understood it. you said: now i have to make the string "and and and" in to "and". In order to do that i have that led me to believe you wanted "and" instead of "and and and" $str="php and and and perl"; $replace=preg_replace("/and/","",$str); echo $replace; Quote Link to comment https://forums.phpfreaks.com/topic/111788-string-replace-problem/#findComment-573903 Share on other sites More sharing options...
jeeva Posted June 25, 2008 Author Share Posted June 25, 2008 i am sorry Crayon Violent.... Your code is working...i just made a small mistake with ur code... really sorry... Thanks a lot Crayon Violent Quote Link to comment https://forums.phpfreaks.com/topic/111788-string-replace-problem/#findComment-573905 Share on other sites More sharing options...
.josh Posted June 25, 2008 Share Posted June 25, 2008 ah okay cool Quote Link to comment https://forums.phpfreaks.com/topic/111788-string-replace-problem/#findComment-573906 Share on other sites More sharing options...
jeeva Posted June 25, 2008 Author Share Posted June 25, 2008 a small problem Crayon Violent.. If the string is like $str="php and and and perl and html" then it gives "php perl and html" but it should be like "php and perl and html" Quote Link to comment https://forums.phpfreaks.com/topic/111788-string-replace-problem/#findComment-573911 Share on other sites More sharing options...
jeeva Posted June 25, 2008 Author Share Posted June 25, 2008 No one......................... :'( Quote Link to comment https://forums.phpfreaks.com/topic/111788-string-replace-problem/#findComment-573915 Share on other sites More sharing options...
sasa Posted June 25, 2008 Share Posted June 25, 2008 try <?php $a = 'php and and and perl and and html. Is it it OK OK?'; $out = preg_replace('/((\w+)\W)(?=\2)/i','', $a); print_r($out); ?> Quote Link to comment https://forums.phpfreaks.com/topic/111788-string-replace-problem/#findComment-574117 Share on other sites More sharing options...
.josh Posted June 25, 2008 Share Posted June 25, 2008 a small problem Crayon Violent.. If the string is like $str="php and and and perl and html" then it gives "php perl and html" but it should be like "php and perl and html" When you presented your problem, I wondered whether or not $str was just an example string representing a 'real' variable input. To that extent, I did know ahead of time that this code would not work in that capacity. But since you didn't mention anything about that, all I could do was assume that that was the exact format of your string. I know that sounds a lot like pointing the finger at you so as to absolve any err on my part, and to an extent it is, but I did disclaim from the get-go that I suck at regex. Though I was sure (as sasa showed us) there was a more elegant solution, you only presented the problem of making "and and and" turn into "and" you did not really explain the full scope of the target string to regex. It is very important to be very specific about the problem at hand, especially when it comes to things like regex. After all, you know how it goes....Garbage In, Garbage Out. Quote Link to comment https://forums.phpfreaks.com/topic/111788-string-replace-problem/#findComment-574242 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.