$content = preg_replace_callback('#(\<code[^>]*\>)(.+)\<\/code\>#s',create_function('$matches','return $matches[1] . htmlentities($matches[2])."</code>";'),$content);
I'm not clear on why < and > are being escaped in your pattern, is that necessary? Try removing the escapes there to make it: (<code[^>]*>)
It should work fine I think.
Note that \<code[^>]*\>
means: <code
plus anything that is is not >
then close >
as a match.
You're now getting two matches:(\<code[^>]*\>)
and (.+)
and using them to create your result.
Note that (.+?) makes little sense, you probably want: (.*) which means 0 or more instances of anything, then the pattern has to find a closing code tag after that anything. This works well if there is only one </code>
possible in the input value, if there can the more than one, you will run into problems because the pattern will probably read all the way to the last one.
This is the version I'd use. (.*) handles the case where there is nothing between the code tags.
$content = preg_replace_callback('#(<code[^>]*>)(.*)</code>#s',create_function('$matches','return $matches[1] . htmlentities($matches[2])."</code>";'),$content);
...`. Also, PHP has anonymous functions
– Phil Jan 19 '17 at 03:00