Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

wwwxxxAPC, How to unsubscrube from dategen spam, WWWXXXAPC, docmd.close 2585, WWWXXXDOCO, nu vot, dhcpd lease file "binding state", WWWXXXDOCO, how to setup procmail to process html2text, how to setup procmail html2text

Links

XODOX
Impressum

#1: preg help

Posted on 2008-04-23 18:50:54 by Claudio Lanzi

I am not well versed on regular expressions, but have fumbled through
enough to come up with these three preg_replace() statements (I'm not
even sure how the third one works, but it does)to reduce a form field
value to the '(###) ###-####' U.S. phone format. My question, can I
combine them into one?

$phone=preg_replace('/\D/','',$text); // strip all non-digits
$phone=preg_replace('/^[0-1]/','',$text); // strip leading 0 or 1
$phone=preg_replace('/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-.
]?([0-9]{4})/', '(\1) \2-\3', $text);

Report this message

#2: Re: preg help

Posted on 2008-04-24 01:28:41 by Alexey Kulentsov

William Gill wrote:
> I am not well versed on regular expressions, but have fumbled through
> enough to come up with these three preg_replace() statements (I'm not
> even sure how the third one works, but it does)to reduce a form field
> value to the '(###) ###-####' U.S. phone format. My question, can I
> combine them into one?
>
> $phone=preg_replace('/\D/','',$text); // strip all non-digits
> $phone=preg_replace('/^[0-1]/','',$text); // strip leading 0 or 1
> $phone=preg_replace('/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-.
> ]?([0-9]{4})/', '(\1) \2-\3', $text);

no problem:

<?php

// only 10 digits plus 0 or 1 at begin
$phone1="sf fd f0 (1-2-3) - 45 6 - 7 :) 89-0 ff";
$phone2=$phone1;

// two expression example
$phone2=preg_replace('/^(\\d{3})(\\d{3})(\\d{4})$/','(\\1) \\2-\\3',
preg_replace('/(?:^\\D*[01]|\\D)/','',$phone2));

echo $phone2."\n";

// one expression example
$phone1=preg_replace('/^\\D*[01]?'.str_repeat('\\D*(\\d)',10 ).'\\D*$/','(\\1\\2\\3)
\\4\\5\\6-\\7\\8\\9\\10',$phone1);

echo $phone1."\n";

?>

Report this message

#3: Re: preg help

Posted on 2008-04-24 17:01:09 by Claudio Lanzi

Alexey Kulentsov wrote:

> no problem:
>
> <?php
>
> // only 10 digits plus 0 or 1 at begin
I am removing 0 and 1 from the beginning because 1) I don't need to
store 1 + phonenumber, just phonenumber, and 2) no valid area code
begins with 0 or 1.

> $phone1="sf fd f0 (1-2-3) - 45 6 - 7 :) 89-0 ff";

What is this? and where do you use it? It just produces the string "sf
fd f0 (1-2-3) - 45 6 - 7 :) 89-0 ff" which I don't see you use anywhere.

> $phone2=$phone1;
>
> // two expression example
> $phone2=preg_replace('/^(\\d{3})(\\d{3})(\\d{4})$/','(\\1) \\2-\\3',
> preg_replace('/(?:^\\D*[01]|\\D)/','',$phone2));
>
> echo $phone2."\n";
>
> // one expression example
> $phone1=preg_replace('/^\\D*[01]?'.str_repeat('\\D*(\\d)',10 ).'\\D*$/','(\\1\\2\\3)
> \\4\\5\\6-\\7\\8\\9\\10',$phone1);
>
doesn't seem to work for me. I realized my example was wrong:

$phone=preg_replace('/\D/','',$text); // strip all non-digits
$phone=preg_replace('/^[0-1]/','',$text); // strip leading 0 or 1
$phone=preg_replace('/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-.
]?([0-9]{4})/', '(\1) \2-\3', $text);

should have been:

$phone=preg_replace('/\D/','',$text); // strip all non-digits
$phone=preg_replace('/^[0-1]/','',$phone); // strip leading 0 or 1
$phone=preg_replace('/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-.
]?([0-9]{4})/', '(\1) \2-\3', $phone);


> echo $phone1."\n";
>
> ?>

Report this message