2

I want to replace a word with PHP preg_replace. It should not replace the word if the characters /> exist before this word.

i.e.

this should be replaced if matched

$word = "foo";

this should not be replaced if matched

$word = "/>foo";
williamcarswell
  • 673
  • 2
  • 5
  • 16

2 Answers2

8

Here you are ;-)

(?<!/>)foo

good luck

Fischermaen
  • 12,238
  • 2
  • 39
  • 56
0
if ($word[0] != '/' && $word[1] != '>') {
    $new_string = str_replace($word, $replace, $string);
}