谁能帮我解释一下这个正则表达式的含义吗?
$html = preg_replace("# <(?![/a-z]) | (?<=\s)>(?![a-z]) #exi", "htmlentities('$0')", $html);
有人将其添加到 How to strip tags in a safer way than using strip_tags function?但我无法理解。
这是我在 stackoverflow 上的第一篇文章,所以如果我犯了任何错误,请原谅我。
谢谢!
最佳答案
#...# the # and # are just characters to start en end a REGEX
(you can use a lot of character for this)
#exi the e, x and i flags. See the PHP.net site for information
about it
< the < character
(?!...) a negative lookahead. The REGEX matches when the characters
after this are NOT equal to one of those
[/a-z] a character class, matches for the / character and the
letters a - z
| OR
(?<=\s) a positive lookbehind. The REGEX maches when there is
\s (whitepspace) before
> the > character
(?![a-z]) negative lookahead for the letters a - z
所以基本上,它匹配所有 <
和>
不用作标签的字符。例如,<foo
和</foo
与 foo>
不匹配也不会。但是1 < 3
会匹配。这将被传递到 htmlentities
功能并成为1 < 3
。现在,您可以省钱地使用 strip_tags
仅删除标签。
关于php - 需要正则表达式的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15760321/