I’m new to pattern matching, having finally figured it out. I am stuck trying to find an approach to the following problem.
I need to return a match (with php preg_match) if any of a number html tags are present.
<p></p>
<br>
<h1></h1>
<h2></h2>
And return no match match otherwise. So anything not in the above list fails, e.g:
<script></script>
<table></table>
ect
…And ideally I want to operate a white list of safe tags if possible.
Anyone know a pattern that I can use/adapt?
Breaking down the expression
The first
/is the delimiterthe
<is the start of the tag, the very first<the
([a-z]*)starts to match a tag name so fir instance < strongthe
\b[^>]*says once you found a space, keep looking for all wordsthe
>says it want the previous section to keep looking until it finds the very first>the
(.*?)says keep on looking and COLLECT ( .. ) the string inside but becuse we have a?then stop looking when you find the next char after the closing brace.the
</\1>says i want to match but only if the value inside is the same as the very first match, this is done by\1as in match, the value of this would be what's found with([a-z]*)`.then you can use preg_match_all to find all them with contents, the array output would be something like
Hope it helps 🙂
Exmaple