PHP正则替换函数preg_replace()报错:Notice Use of undefined constant的解决方法 PHP正则替换函数preg_replace()报错:Notice Use

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

PHP正则替换函数preg_replace()报错:Notice Use of undefined constant的解决方法 PHP正则替换函数preg_replace()报错:Notice Use

风起从容   2021-03-24 我要评论
想了解PHP正则替换函数preg_replace()报错:Notice Use of undefined constant的解决办法分析的相关内容吗,风起从容在本文为您仔细讲解PHP正则替换函数preg_replace()报错:Notice Use of undefined constant的解决办法的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:PHP,正则,替换,函数,preg_replace(),报错,Notice,Use,of,undefined,constant,解决办法,下面大家一起来学习吧。

本文实例讲述了PHP正则替换函数preg_replace()报错:Notice Use of undefined constant的解决方法。分享给大家供大家参考,具体如下:

环境错误级别:error_reporting =E_ALL

某天我在研究一下php下的正则替换函数perg_replace(),

示例:

代码:

$subject="2222<b>a</b>2222fff222222222A22222";
$pattern = "/(a)/e";
$replacement= "md5($1)";//$1,取匹配到的内存变量的值(\1也可以,只不过要注意双引号内的转义)
echo preg_replace($pattern,$replacement, $subject);

结果:

虽然结果是对的,但是报了如下的错误:

Notice: Use of undefined constant a - assumed 'a' in D:\xampp\htdocs\studyRoom\regular\index.php(18) : regexp code on line 1

造成这原因的是在$replacement= "md5($1)";中的md5()方法,在参数传递的时候,参数没有加单引号或者双引号,系统就认为是个常量,所以就出现了这样的问题。改成如下 即可:

$replacement= "md5('$1')";(或:$replacement= "md5('\\1')";)

注:在取匹配到的内存变量的值的时候可以用"$1"或者"\1"这样的形式.

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.softyun.net/regex/javascript

正则表达式在线生成工具:
http://tools.softyun.net/regex/create_reg

希望本文所述对大家PHP程序设计有所帮助。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们