Reputation: 3501
i wrote a script that search a html content and replace some keywords with linked keyword
Code:
$text = '<p>hello</p>
<img src="hello.png" />';
echo str_replace('hello', '<a href="hello">hello</a>', $text);
Result:
<p><a href="hello">hello</a></p>
<img src="<a href="hello">hello</a>.png" />
now you can see the src attribute of image is manipulated an i dont want this.
how to exclude img tag html tags being searched for replace?
any help will be appreciated
Upvotes: 0
Views: 1623
Reputation: 782
try regex
$text = '<p>hello</p>
<img src="hello.png" />';
echo preg_replace("/hello(?!.(png|jpg))/", "<a href='hello'>here</a>", $text);
Upvotes: 0
Reputation: 11
Simpler solution
echo str_replace('<p>hello</p>', '<a href="hello"></a>', $text);
Wide range REGEX solution
Check this : http://www.thatsquality.com/articles/how-to-match-and-replace-content-between-two-html-tags-using-regular-expressions
PHPDOM solution
Check this : http://php.net/manual/en/book.dom.php
Upvotes: 1