numbers only

0 views
Skip to first unread message

Jimboidaho

unread,
Sep 6, 2010, 6:41:42 PM9/6/10
to Professional PHP Developers
I am looking for a way to clean up phone numbers before storing. I
have not found a good way to do this and I am not very good at regular
expressions. Here is what I have so far.

$pattern = '/\d*/';
preg_match_all($pattern,'123-456-7890', $matches);
$junk = implode('',$matches[0]);
echo $junk;
echo '<br />';

Seems there should be a better way.

Thanks.

Jack Timmons

unread,
Sep 6, 2010, 6:43:46 PM9/6/10
to professi...@googlegroups.com
On Mon, Sep 6, 2010 at 5:41 PM, Jimboidaho <jimbo...@gmail.com> wrote:
> I am looking for a way to clean up phone numbers before storing.  I
> have not found a good way to do this and I am not very good at regular
> expressions.  Here is what I have so far.
>
>        $pattern = '/\d*/';
>        preg_match_all($pattern,'123-456-7890', $matches);
>        $junk = implode('',$matches[0]);
>        echo $junk;
>        echo '<br />';

$string = "999-999-9999";
$string = preg_replace("/\D/", "", $string);

--
Jack Timmons
@_Codeacula

Robert Gonzalez

unread,
Sep 6, 2010, 8:55:07 PM9/6/10
to professi...@googlegroups.com
Are you trying to get just the numbers out of the string?

> --
> This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.
>
> For information or project assistance please visit :
> http://www.360psg.com
>
> You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
> To post to this group, send email to Professi...@googlegroups.com
> To unsubscribe from this group, send email to Professional-P...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/Professional-PHP

Jack Timmons

unread,
Sep 6, 2010, 8:57:18 PM9/6/10
to professi...@googlegroups.com
On Mon, Sep 6, 2010 at 7:55 PM, Robert Gonzalez
<robert.anth...@gmail.com> wrote:
> Are you trying to get just the numbers out of the string?

That's what I based my answer off of.

If OP's trying to validate the ring-a-ling number, well, that'll take
a bit more than I have up my t-shirt sleeves.

--
Jack Timmons
@_Codeacula

Ovidiu Alexa

unread,
Sep 7, 2010, 2:27:58 AM9/7/10
to professi...@googlegroups.com
$string='My Phone Number is 1234-000-111';
echo intval($string);
//1234000111

Cheers!

Jimboidaho

unread,
Sep 7, 2010, 9:41:26 AM9/7/10
to Professional PHP Developers
That's what I was looking for. I like storing phone numbers with only
the numbers. Then I can have a consistent format when I display them
on the page.

Thanks.

On Sep 6, 4:43 pm, Jack Timmons <codeac...@codeacula.com> wrote:

Jimboidaho

unread,
Sep 7, 2010, 9:45:02 AM9/7/10
to Professional PHP Developers
Hi Ovidui,

This doesn't seem to work for me.
echo intval('123-456-7890');
returns an answer of 123

Robert Gonzalez

unread,
Sep 7, 2010, 12:21:50 PM9/7/10
to professi...@googlegroups.com
On the manual page for intval() there is this:

Strings will most likely return 0 although this depends on the leftmost characters of the string.

The way intval() works is to look at the string. If the left most char is a number it will continue to grab the numbers in the string until it comes to a non-numeric char value. Everything it has seen up to that point is returned. If the left most char is a non-numeric value it returns 0. 

For what you're doing I wouldn't use intval(). I'd use a preg_replace on all non numerics, like what Jack posted earlier.

Reply all
Reply to author
Forward
0 new messages