[solved] Remove non-numbers

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

[solved] Remove non-numbers

Post 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?
Last edited by pickle on Fri Sep 22, 2006 2:44 pm, edited 1 time in total.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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... :?
User avatar
mibocote
Forum Newbie
Posts: 18
Joined: Sun Aug 20, 2006 9:51 am
Contact:

Post by mibocote »

I think you meant:

Code: Select all

$phone_number="888-5555";
$phone_number = preg_replace('/[^0-9]/','',$phone_number);
echo $phone_number
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Holy cow! How did I miss that? Man I need the weekend.

Thanks.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mibocote
Forum Newbie
Posts: 18
Joined: Sun Aug 20, 2006 9:51 am
Contact:

Post by mibocote »

pickle wrote:Holy cow! How did I miss that? Man I need the weekend.

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