I have this preg_replace patterns and replacements :
$patterns = array(
"/<br\W*?\/>/",
"/<strong>/",
"/<*\/strong>/",
"/<h1>/",
"/<*\/h1>/",
"/<h2>/",
"/<*\/h2>/",
"/<em>/",
"/<*\/em>/",
'/(?:\<code*\>([^\<]*)\<\/code\>)/',
);
$replacements = array(
"\n",
"[b]",
"[/b]",
"[h1]",
"[/h1]",
"[h2]",
"[/h2]",
"[i]",
"[/i]",
'[code]***HTML DECODE HERE***[/code]',
);
In my string I want to html_entity_decode the content between these tags :
<code> < $gt; </code> but keep my array structure for preg replace
so this : <code> < > </code> will be this : [code] < > [/code]
Any help will be very appreciated, thanks!
You cannot encode this in the replacement string. As PoloRM suggested, you could use
preg_replace_callbackspecifically for your last replacement instead:Equivalently, using
create_function:Or, as of PHP 5.3.0:
But note that in all three cases, your pattern is not really optimal. Firstly, you don’t need to escape those
<and>(but that is just for readability). Secondly, your first*allows infinite repetition (or omission) of the lettere. I suppose you wanted to allow attributes. Thirdly, you cannot include other tags within your<code>(because[^<]will not match them). In this case maybe you should go with ungreedy repetition instead (I also changed the delimiter for convenience):As you can already see, this is still far from perfect (in terms of correctly matching the HTML in the first place). Hence, the obligatory reminder: don’t use regex to parse HTML. You will be much better off, using a DOM parser. PHP brings a built-in one, and there is also this very convenient-to-use 3rd-party one.