What is the easiest way of applying highlighting of some text excluding text within OCCASIONAL tags "<...>"?
CLARIFICATION: I want the existing tags PRESERVED!
$t =
preg_replace(
"/(markdown)/",
"<strong>$1</strong>",
"This is essentially plain text apart from a few html tags generated with some
simplified markdown rules: <a href=markdown.html>[see here]</a>");
Which should display as:
"This is essentially plain text apart from a few html tags generated with some simplified markdown rules: see here"
... BUT NOT MESS UP the text inside the anchor tag (i.e. <a href=markdown.html>
).
I've heard the arguments of not parsing html with regular expressions, but here we're talking essentially about plain text except for minimal parsing of some markdown code.
You could split the string into tag/no-tag parts using
preg_split
:Then you can iterate the parts while skipping every even part (i.e. the tag parts) and apply your replacement on it:
At the end put everything back together with
implode
:But note that this is really not the best solution. You should better use a proper HTML parser like PHP’s DOM library. See for example these related questions:
Actually, this seems to work ok:
A string like
$item="odd|string"
would cause some problems, but I won't be using that kind of string anyway... (probably needs htmlentities(...) or the like...)You could split your string into an array at every '<' or '>' using
preg_split()
, then loop through that array and replace only in entries not beginning with an '>'. Afterwards you combine your array to an string usingimplode()
.actually this is not very efficient, but it worked for me
This regex should strip all HTML opening and closing tags:
/(<[.*?]>)+/
You can use it with preg_replace like this: