php正则表达式转义特殊字符[复制]

前端之家收集整理的这篇文章主要介绍了php正则表达式转义特殊字符[复制]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > 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.

参考相关链接

The 07003 function may be used to escape a string for injection into a pattern and its optional second parameter may be used to specify the delimiter to be escaped.

原文链接:https://www.f2er.com/php/135591.html

猜你在找的PHP相关文章