0

suppose I have $str="nikparsa neginnikparsa somenikparsa" when I use

$str= str_replace('nikparsa', 'nik parsa', $str);
echo $str;

It will replace neginnikparsa with neginnik parsa too

I just want to replace when nikparsa is a distinct word

I mean like \bnikparsa\b in pregmatch but I don't know how to write it.

Nickool
  • 3,662
  • 10
  • 42
  • 72

2 Answers2

6

Use preg_replace:

$str = preg_replace('/\bnikparsa\b/i', 'nik parsa', $str);
simshaun
  • 21,263
  • 1
  • 57
  • 73
0

Well, if you really don't want to use preg_replace, then:

substr( str_replace( ' stackoverflow ', '  do you see the apples?  ', " $str "), 1, -1 );

Read the comments about its limitations :)

biziclop
  • 14,466
  • 3
  • 49
  • 65