0

Possible Duplicate:
PHP string replace match whole word

What I want to do is to replace Tomato with Apple for example. The Probleim I'm running in is:

when the string contains "Tomatos" (mention the s) it will replace Tomato and leafe me with a string "Apples" (again mention the s).

Because "Tomato" != "Tomatos" (...s) it shall NOT replace.

Which PHP funktion is good for this task? I tried the str_replace function but it seems like it isn't possible to archive my task with it.


The words are seperated with ",".

Community
  • 1
  • 1
cooljoe
  • 21
  • 2
  • 1
    exact duplicate of http://stackoverflow.com/questions/3426265/php-string-replace-match-whole-word – kapa Mar 08 '11 at 11:37

2 Answers2

2

preg_replace. You can search for a regular expression, like \bTomato\b, which means whole word Tomato.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

If you want to use str_replace try adding commas either side of the Tomato. Then it should only replace the word Tomato.

Otherwise you'll need preg_replace

Belinda
  • 1,230
  • 2
  • 14
  • 25