gdfhghjdfghgfhf Posted April 9, 2008 Share Posted April 9, 2008 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 https://forums.phpfreaks.com/topic/100275-need-help-with-preg_replace-or-str_replace/ Share on other sites More sharing options...
darkfreaks Posted April 9, 2008 Share Posted April 9, 2008 str_replace Link to comment https://forums.phpfreaks.com/topic/100275-need-help-with-preg_replace-or-str_replace/#findComment-512708 Share on other sites More sharing options...
gdfhghjdfghgfhf Posted April 9, 2008 Author Share Posted April 9, 2008 could you give me an example plz? i'm newbie with the syntax thing... and btw what is the difference between preg_ and str_ ? Link to comment https://forums.phpfreaks.com/topic/100275-need-help-with-preg_replace-or-str_replace/#findComment-512712 Share on other sites More sharing options...
awpti Posted April 9, 2008 Share Posted April 9, 2008 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 https://forums.phpfreaks.com/topic/100275-need-help-with-preg_replace-or-str_replace/#findComment-512715 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.