加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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.

参考相关链接

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读