<p>I'm writing an <code>RSS</code> to <code>JSON parser</code> and as a part of that, I need to use <code>htmlentities()</code> on any tag found inside the description tag. Currently, I'm trying to use <code>preg_replace()</code>, but I'm struggling a little with it. My current (non-working) code looks like:</p>

<pre><code>$pattern[0] = "/\&lt;description\&gt;(.*?)\&lt;\/description\&gt;/is";
$replace[0] = '&lt;description&gt;'.htmlentities("$1").'&lt;/description&gt;';
$rawFeed = preg_replace($pattern, $replace, $rawFeed);
</code></pre>

<p>If you have a more elegant solution to this as well, please share. Thanks.</p>

Simple.  Use
```
preg_replace_callback
```
:
```
```
function _handle_match($match)
{
    return '<description>' . htmlentities($match[1]) . '</description>';
}

$pattern = "/\<description\>(.*?)\<\/description\>/is";
$rawFeed = preg_replace_callback($pattern, '_handle_match', $rawFeed);
```
```
It accepts any callback type, so also methods in classes.