preg help

preg help

am 23.04.2008 18:50:54 von 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);

Re: preg help

am 24.04.2008 01:28:41 von 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:


// 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";

?>

Re: preg help

am 24.04.2008 17:01:09 von Claudio Lanzi

Alexey Kulentsov wrote:

> no problem:
>
> >
> // 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";
>
> ?>