williamcarswell
williamcarswell

Reputation: 673

PHP preg_replace word but not if there is certain characters preceding the word

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";

Upvotes: 2

Views: 1520

Answers (2)

user743234
user743234

Reputation:

if ($word[0] != '/' && $word[1] != '>') {
    $new_string = str_replace($word, $replace, $string);
}

Upvotes: 0

Fischermaen
Fischermaen

Reputation: 12458

Here you are ;-)

(?<!/>)foo

good luck

Upvotes: 8

Related Questions