0

I am using the following to use htmlentities on anything inside of a code tag:

$content = preg_replace_callback('#\<code\>(.+?)\<\/code\>#s',create_function('$matches','return "<code>".htmlentities($matches[1])."</code>";'),$content);

This way I can echo it out onto the page. This works great, other than it only works if the code tag is exactly <code>. How can I update the regex so that it will work with the contents inside of <code + anything + >?

For example:

<code>
<code class="someclass">
<code class="someclass" data-attr="value">
Chathurika Senani
  • 716
  • 2
  • 9
  • 25
MultiDev
  • 10,389
  • 24
  • 81
  • 148
  • If you must use regex ~ `#...`. Also, PHP has anonymous functions – Phil Jan 19 '17 at 03:00
  • @Phil I'd like to see how this is a duplicate of that other question. And even if it was, there doesn't seem to be a clear answer on that linked question. There are dozens of answers with dozens of opinions for a question that is not a duplicate of this one. – MultiDev Jan 19 '17 at 03:01
  • the pattern has to exclude >, see my answer. – Lizardx Jan 19 '17 at 03:01
  • I am unfamiliar with these anonymous functions. Care to point me in the right direction? – MultiDev Jan 19 '17 at 03:02
  • Urgh, I'm really sorry about that. Was meant to link to this post ~ http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php – Phil Jan 19 '17 at 05:11

1 Answers1

2
$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);
Lizardx
  • 1,165
  • 10
  • 16