doumu6997 2014-05-28 13:37
浏览 44
已采纳

使用下划线字符时,php preg_replace返回0值

My question is, why does the position of the '_' (underscore) character cause this problem?

I have inherited a script that is using php's preg_replace in a function. The regex used is returning a 0 on any number it is used on.

function foo($number){ 
  $number = preg_replace('/[a-z$,-_]/i','',$number);
  // more code...
}

I did a bunch of debugging and found the problem was with the preg_replace(). A co-worker mentioned that the order of the characters in the regex maybe causing the bug. So, I played with this and found it to be true. The position of the '_' (underscore) character is the sinister culprit. I changed this to:

'/[a-z$_,-]/i'

... and everything works fine.

So, the question, again, is why does the position of the '_' (underscore) character cause this problem? I've Googled on this but have not found it and I thought the minds in this forum may have the answer.

Thanks for any enlightenment! -jc

  • 写回答

2条回答 默认 最新

  • dsgdf45654 2014-05-28 13:40
    关注

    Some characters need to be escaped like this

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

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败