谁能给我解释一下这个正则表达式的含义?
$html = preg_replace("# <(?![/a-z]) | (?<=s)>(?![a-z]) #exi", "htmlentities('$0')", $html);
有人在如何以比使用strip_tags功能更安全的方式剥离标签?但我不能理解。
这是我在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
仅删除标记。
在我看来,它试图确定什么不是HTML标签,仅仅基于是否在<或者>是一个数字。或者>
这意味着它将捕获以下<
:
<span>This is <5 ml.</span>
并将其替换为该字符的HTML实体等效,允许您安全地使用strip_tags
而不会破坏字符串的含义(如参考问题中所讨论的)。
查找没有后跟a-z
的<
或
空格后跟>
,不能后跟a-z
然后将其替换为htmlentities('$0')
,其中$0是您的整个匹配!
i
选项忽略大小写
e
进行正常替换
x
忽略空白文字