php - 需要正则表达式的解释

标签 php regex

谁能帮我解释一下这个正则表达式的含义吗?

$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</foofoo> 不匹配也不会。但是1 < 3会匹配。这将被传递到 htmlentities功能并成为1 &lt; 3 。现在,您可以省钱地使用 strip_tags仅删除标签。

关于php - 需要正则表达式的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15760321/

相关文章:

javascript - 包含 1 个引用的正则表达式与所需字符串不匹配

php - 通过id获取内容并通过url显示php mysql

php - 使用 php 在 android 上打开实时音频流

Java正则表达式查找数字

javascript - 删除所有出现的新行和制表符

java - 使用 java 从字符串中查找 mathml

用于在文本中查找图像的 php 正则表达式

c# - Windows.Web.Http.HttpClient.ReadAsStringAsync() 导致 COMException HRESULT E_FAIL

当我执行 PHP/HTML 站点时,它显示为空。没有错误,只是空的

php - 将值存储在数组内的精确索引中