Reputation: 4862
I'm trying to match any open HTML tag except input
tag using regular expression in PHP. Here is my pattern.
/<([a-z]+)([^>]*>)?/i
It matches all below:
<input type="text">
<img src=">
<a href="">
<button type="button"></button>
<div id="some"></div>
<p></p>
I don't want to match input
. I may exclude more tags in the future as I stated some tags in my question title.
As per my example, I also want to keep the tag name only returned in the matched results, e.g, img
, a
, button
, div
, p
, etc.
Upvotes: 1
Views: 1588
Reputation: 67968
<(?:(?!input)[^>])*>(?:<\/[^>]*>)?
Try this.See demo.
https://www.regex101.com/r/fG5pZ8/13
$re = "/<(?:(?!input)[^>])*>(?:<\\/[^>]*>)?/im";
$str = "<input type=\"text\">\n<img src=\">\n<a href=\"\">\n<button type=\"button\"></button>\n<div id=\"some\"></div>\n<p></p>";
preg_match_all($re, $str, $matches);
Edit:
Use
(?!<input)<([A-Z0-9a-z]+)([^>]*>)?
If you want to save tag separately.
https://www.regex101.com/r/fG5pZ8/16
Upvotes: 2
Reputation: 43136
Use a negative lookahead like (?!input\b)
:
<(?!input\b)([\w]+)([^>]*>)?
To exclude multiple tags, use (?!(?:tag1|tag2|tag3|...)\b)
Upvotes: 2