Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/111788-string-replace-problem/
Share on other sites

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".

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;

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.

 

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.