源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-11-17 09:50 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:PHP正则替换函数preg_replace()报错:Notice Use of undefined constant的解决方法分析
本文实例讲述了PHP正则替换函数preg_replace()报错:Notice Use of undefined constant的解决方法。分享给大家供大家参考,具体如下: 环境错误级别:error_reporting =E_ALL 某天我在研究一下php下的正则替换函数perg_replace(), [b]示例:[/b] 代码:
$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')";) [b]注:在取匹配到的内存变量的值的时候可以用"$1"或者"\1"这样的形式.[/b] [b]PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:[/b] [b]JavaScript正则表达式在线测试工具: [/b][url=http://tools.jb51.net/regex/javascript]http://tools.jb51.net/regex/javascript[/url] [b]正则表达式在线生成工具: [/b][url=http://tools.jb51.net/regex/create_reg]http://tools.jb51.net/regex/create_reg[/url] 更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/180.htm]php正则表达式用法总结[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/348.htm]PHP基本语法入门教程[/url]》、《[url=http://www.1sucai.cn/Special/357.htm]PHP运算与运算符用法总结[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/495.htm]PHP网络编程技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部