Jump to content

need help with preg_replace or str_replace


gdfhghjdfghgfhf

Recommended Posts

ok well i have a variable that includes a bunch of words

exemple: "black cat blue fish brown dog brown horse black dog"

 

i.e. i want to take out the words "brown dog"... these 2 words have to be next to each others and can be located anywhere in the string

 

which one should i use? preg_replace or str_replace?

could anyone give me an example how i can do that?

To expand upon the above answer (which is quite correct):

 

1.) preg_replace is slower (memory and cpu expensive) compared to str_replace

2.) If you know exactly what you're replacing and with what - str_replace is easier

  ex:

 

str_replace("my_string", "my_replacement_string", "this is my_string"); would produce: this is my_replacement_string

 

regex is a search pattern. You aren't searching if you know what must be replaced.

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.