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?

Link to comment
Share on other sites

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.

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.