php正则表达式转义特殊字符[复制]
发布时间:2020-12-13 18:22:38 所属栏目:PHP教程 来源:网络整理
导读:参见英文答案 Is there a PHP function that can escape regex patterns before they are applied?1个 我写了下面的代码(是的,它确实有效),并想知道为什么我不需要逃避’'和'‘模式中的字符,因为它们被php手册视为“特殊”字符. http://www.php.net/manual/e
参见英文答案 >
Is there a PHP function that can escape regex patterns before they are applied?1个
我写了下面的代码(是的,它确实有效),并想知道为什么我不需要逃避’<'和'>‘模式中的字符,因为它们被php手册视为“特殊”字符. http://www.php.net/manual/en/function.preg-quote.php var_dump(preg_match('/<[A-Za-z][A-Za-z0-9]*>/',"<html>",$matches)); echo "<pre>"; var_dump(htmlentities($matches[0])); echo "</pre>"; 输出: int(1) string(12) "<html>"
只有
this page中列出的字符需要在PHP正则表达式匹配/替换中进行转义.
而<和>可以作为delimiter,它不需要在给定的示例中进行转义,因为您已经/(斜杠)充当delimiter. 参考相关链接
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |