使用下划线字符时,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

Some characters need to be escaped like this

[a-z,\-_]

It's the position of the hyphen, not the underscore. With [a-z$,-_], you're inadvertently creating a character range from , to _. Put the hyphen on the end or escape it.

Comma , is ASCII 0x2C, underscore _ is 0x5F, and digits fall between those (0x30 to 0x39).
(ref: ASCII table)