Replace all quotes (") inside HTML tags using PHP

PostMar 31st, 2010 | Comments (0)
If you've ever stored HTML tags in a database and filtered the contents through PHP's htmlentities(), you've probably run into the problem of the quotes inside the tags being converted into " entities.

Example:

<a href="http://www.epigroove.com/">Epigroove.com</a>

turns into:

<a href=&quot;http://www.epigroove.com/&quot;>Epigroove.com</a>

Most browsers can handle this, but IE can not.

Solution:

$text = preg_replace('/<([^<>]+)>/e', '"<" .str_replace("&quot;", \'"\', "$1").">"', $text);

This replaces all instances of '&quot;' inside HTML tags.

Comments

There have been no comments

Post a comment

Name
URL
Email
Comment