user2291765
user2291765

Reputation: 3

php regular expression find and replace all except anchor and images tags

<p>$html = "this is an anchor go this again to<p> and this image";</p><p>find 'this' and replace it with 'a new word' and the result will be</p> <p>// a new word is an anchor go this again to<p> and a new word image to replace all except anchor and images tags</p>

Upvotes: 0

Views: 252

Answers (1)

Phil Cross
Phil Cross

Reputation: 9302

Your code needs to be properly formatted so it is readable.

As for the regex, you don't need it, you can use strip_tags() with the second parameter:

$text = '<a href="">link<img src=""></a><span>some text</span>';

echo strip_tags($text, '<a><img>');

The second parameter in strip_tags tells the function what tags you want to keep, in this case, tags and tags.

That is to replace actual tags.

To replace words, use str_ireplace()

$text = 'this is a link, this is an image';

echo str_ireplace('this', 'new word', $text);

// Outputs:
    new word is a link, new word is an image

If you need a different answer, try re-phrasing your original question to make it more informative

Upvotes: 1

Related Questions