Your input string ( minorly edited to clarify my answer):
$string = "Just simply put
<code>
<p>encoded code</p>
</code>
and this <b>bold</b> text will be bold,
because it isn't inside <code><b>code tags</b></code>";
Step 1:
Break your string into parts as surrounded by by <code>
. Note your regex should use #
not /
as the delimiters so you don't need to care about the /
in your </code>
.
preg_match_all("#<code>(.*?)</code>#is", $string, $codes);
Note the s
at the end of the REGEX for ignoring line breaks on the group (*)
.
The above code is lazy (see links at the bottom) and will also not match incomplete tags (such as <code>
with no corresponding </code>
).
Step 2:
Make the HTML changes as required to each of the found substrings (You should be familiar with how preg_match_all
returns the data from the function, see link at the bottom):
$replace = [];
foreach($codes[1] as $key=>$codeBlock ){
$replace[$key] = htmlentities($codeBlock, ENT_QUOTES, "UTF-8", false);
}
unset($key, $codeBlock);
Step 3:
Apply the changes to the original values (theses are NOT the same as the converted values, used in Step 2):
foreach($codes[0] as $key=>$replacer){
$string = str_replace($replacer, $replace[$key], $string);
}
unset($key, $replacer, $replace);
Output:
The above will then output:
Just simply put
<p>encoded code</p>
and this bold text will be bold, because it isn't inside <b>code tags</b>
You should have a familiar understanding of preg_match_* family of PHP functions as well as general PCRE REGEX.
Please also read this here, and here and read this and especially this.
Cheers
, because I don't want to show it as html, but as plain text (it's what htmlspecialchars() do)
– Dan B. May 15 '18 at 14:51` tags as HTML. (It's basically broken input which is always a nightmare to deal with and requires lots of heuristics).
– Quentin May 15 '18 at 14:53