PHP 正则表达式以匹配除某些标签之外的 HTML 标签名称


PHP regex to match HTML tag names except some tags

我正在尝试匹配任何打开的 HTML 标签,除了 PHP 中使用正则表达式input标签。这是我的模式。

/<([a-z]+)([^>]*>)?/i

它匹配以下所有内容:

<input type="text">
<img src=">
<a href="">
<button type="button"></button>
<div id="some"></div>
<p></p>

我不想匹配input.我将来可能会排除更多标签,因为我在问题标题中陈述了一些标签

到目前为止我尝试过什么

[编辑]

根据我的示例,我还希望仅在匹配的结果中返回标签名称,例如imgabuttondivp等。

<(?:(?!input)[^>])*>(?:<'/[^>]*>)?

试试这个。请参阅演示。

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);

编辑:

(?!<input)<([A-Z0-9a-z]+)([^>]*>)?

如果要单独保存标签。

https://www.regex101.com/r/fG5pZ8/16

使用负面的前瞻,如 (?!input'b)

<(?!input'b)(['w]+)([^>]*>)?

要排除多个标签,请使用(?!(?:tag1|tag2|tag3|...)'b)