PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > preg
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
preg

Réponse
 
LinkBack Outils de la discussion
Vieux 23/04/2008, 17h50   #1
William Gill
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut preg

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);
  Réponse avec citation
Vieux 24/04/2008, 00h28   #2
Alexey Kulentsov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: preg

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

?>
  Réponse avec citation
Vieux 24/04/2008, 16h01   #3
William Gill
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: preg

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

  Réponse avec citation
Vieux 25/04/2008, 00h38   #4
Alexey Kulentsov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: preg

William Gill wrote:
>> $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.


This it phone number with some junk inside to test example. Output is
(123) 456-7890
(123) 456-7890

> doesn't seem to work for me. I realized my example was wrong:

I always test my examples before posting. Did you try just copy
example from post and run it?
  Réponse avec citation
Vieux 25/04/2008, 15h57   #5
William Gill
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: preg

Alexey Kulentsov wrote:
> William Gill wrote:
>>> $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.

>
> This it phone number with some junk inside to test example. Output is


Somehow I missed that.

>> doesn't seem to work for me. I realized my example was wrong:

> I always test my examples before posting. Did you try just copy example
> from post and run it?


I must have cut and pasted wrong, because now when I c&p into a fresh
file it works, thanks.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h12.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11561 seconds with 13 queries