douzhanhui5662 2015-01-04 15:06
浏览 86
已采纳

PHP preg_replace意外输出以匹配特殊字符'$'

I got an Issue in a PHP Script, in case of this function for regular expression matching and replacing. I've tested my regular Expression on www.regex101.com and got always expected results, but not in PHP.

I use this Pattern inside the Function:

$matchedName = preg_replace("/^(\$|\(.+\))( ?)/", "", $name);

To match Character '$' or any Expression in Brackets at the Begin and delete this. Any Input with Brackets works well.

$name = "$ blah";
$matchedName = preg_replace("/^(\$|\(.+\))( ?)/", "", $name);
var_dump($matchedName);

Output:
string(8) "$ blah"

Now, got i missunderstood something? Or got this Function really an Issue in Case of the '$' Character?

Thanks for any Replies

  • 写回答

2条回答 默认 最新

  • drix47193 2015-01-04 15:33
    关注
    $re = "/^(\\$|\\(.+\\))( ?)/im";
    $str = "\$ blah";
    $subst = "";
    
    $result = preg_replace($re, $subst, $str);
    

    Try this.This is working.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Qt中实现子线程和管理线程类之间实时通信
  • ¥15 cacls 命令如何解除锁定文件夹?
  • ¥50 C++使用TWAIN协议如何实现A3幅面扫描仪扫描A4横向
  • ¥15 如何在sql server里完成筛选
  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?