Page 1 of 1

[solved] Remove non-numbers

Posted: Fri Sep 22, 2006 2:22 pm
by pickle
This seems so easy, but it's not working. I'd like to remove any characters that aren't 0,1,2,3,4,5,6,7,8, or 9. This is what I've got so far:

Code: Select all

$phone_number="888-5555";
preg_replace('/[^0-9]/','',$phone_number);
echo $phone_number;
That seems like it should work, but '888-5555' keeps getting output. I tried replacing [^0-9] with ^\d as well, but that didn't work either.

Any ideas?

Posted: Fri Sep 22, 2006 2:35 pm
by Luke
wouldn't that replace anything that is NOT numbers?
EDIT: nevermind I reread the thread title and now I feel like an idiot... :?

Posted: Fri Sep 22, 2006 2:42 pm
by mibocote
I think you meant:

Code: Select all

$phone_number="888-5555";
$phone_number = preg_replace('/[^0-9]/','',$phone_number);
echo $phone_number

Posted: Fri Sep 22, 2006 2:44 pm
by pickle
Holy cow! How did I miss that? Man I need the weekend.

Thanks.

Posted: Fri Sep 22, 2006 2:46 pm
by mibocote
pickle wrote:Holy cow! How did I miss that? Man I need the weekend.

Thanks.
No problem :), I need the weekend too.