8 regular expressions for very useful PHP

In Devolio.com the author made a list of 8 regurales expressions that considers equipment at the time of programming with PHP, many of them used to validate telephone names of user, numbers, directions of email among others.

To validate a user name

This rule is for allowing usuary of 4 up to 28 characters of length, alphanumeric and allowing low scripts.

$string = userNaME4234432_;
if (preg_match (/^ [a-z \ d_] {4.28} $/i, $string)) {
    I throw example 1 successful. ;
}

Telephone numbers
This is for validating telephone numbers (in the format of the United States, Canada and Mexico) the number must be of the following way: (###) ###-####

$string = (032) 555-5555;
if (preg_match (/^ (\ (? [0-9] {3.3} \)? |[0-9] {3.3} [-. ])[] [0-9] {3.3} [-. ]? [0-9] {4.4} $/, $string)) {
I throw successfully realised example 2. ;
}

The telephone number can be written (###) ###-#### thus, or thus ###-###-#### and also is going to validate.

Directions of email

It is not necessary to say plus the code is the following one.

$string = first.last@ domain.co.uk;
if (preg_match (
/^ [^0-9] [a-zA-Z0-9_]   ([.][a-zA-Z0-9_]  ) * [@] [a-zA-Z0-9_]   ([.][a-zA-Z0-9_]  ) * [.][a-zA-Z] {2.4} $/,
$string)) {
    I throw successfully realised example 3. ;
}

Postal codes

Postal codes of which it only allows numbers in following format xxxxx and xxxxx-xxxx

$string = 55324-4324;
if (preg_match (/^ [0-9] {5.5} ([-]? [0-9] {4.4})? $/, $string)) {
    I throw successfully realised example 4. ;
}

Directions IP

We did not need to do a PING nor nothing of the sort, we only verified that or they are written, how this 255.255.255.0 example.

$string = 255.255.255.0;
if (preg_match (
^ (: 25 [0-5]|2 [0-4] \ d|1 \ d \ d|[1-9] \ d|\ d) (: [.](: 25 [0-5]|2 [0-4] \ d|1 \ d \ d|[1-9] \ d|\ d)){3} $,
$string)) {
    I throw successfully realised example 5. ;
}

Hexadecimal colors

Since we also spoke of numbers we can verify hexadecimal values of numbers in its 2 forms the normal one and been brief (#333, 333, #333333 or 333333) with the optional symbol #.

$string = #666666;
if (preg_match (/^# (: (: [high frequency \ d] {3}) {1.2}) $/i, $string)) {
    I throw successfully realised example 6. ;
}

To look for commentaries multi-line

A form simple to look for or to eliminate commentaries multiline in PHP/CSS/Otros languages.

$string = /* commmmment *;
if (preg_match (/^ [(/*)  ,  (*)]$/, $string)) {
    I throw successfully realised example 7. ;
}

Dates
One of the typical formats of dates is the MM/DD/YYYY and its validation is the following one.

$string = 10/15/2007;
if (preg_match (/^ \ d {1.2} \/\ d {1.2} \/\ d {4} $/, $string)) {
    I throw successfully realised example 8. ;
}

You know some other expression regular that useful?

7 Commentaries (yours Adds)

  1. Commented 19 October 2007 to the 18:27 | Permalink | To respond

    Very interesting. I usually program in ASP, and are some small differences. In any case, the regular expressions usually are very useful.

    For agradecerte your aid, I leave this meme by far affection you ;-)

    http://soportetonto.blogspot.com/2007/10/por-que-escribo-este-blog.html

  2. Commented 3 December 2007 to the 14:46 | Permalink | To respond

    Looking for in San google I gave with this page, was the information that it looked for, good tip.

    Greetings from Veracruz, Mexico

  3. Samuel
    Commented 12 February 2008 to the 23:06 | Permalink | To respond

    hey thank you very much from towards 1 hour was looking for the ER of the user thank you very much

  4. Commented 29 March 2008 to the 14:59 | Permalink | To respond

    very good, thank you very much

  5. adriana
    Commented 5 August 2008 to the 12:40 | Permalink | To respond

    I need code to validate an electronic mail

A Commentary writes

Its mail never will be published nor shared. The required fields are noticeable *

*
*

2 Trackbacks (URL of trackback)

  1. [...] Carlos Leopoldo - 8 regular expressions for very useful PHP [...]

  2. By regular Expressions in PHP - elWebmaster.com in 2 Julio 2008 to the 10:10

    [...] Source: Carlos Leopoldo [...]

The people who are leido this article looked for:

to validate I number reason regular expressions - regular expressions for php - to clear commentaries with regular expressions PHP - php regular expressions numbers - preg_match example - regular expression to validate file extension - to validate URL php regular expression - validation of regular expressions - regular expression mail php - php to validate telephone - regular expression to verify mail php - regular expression to validate reason - regular expression date php - regular expressions telephone - examples of regular expressions with URL - regular expression to clear code HTML php - regular expressions mail php - problems with preg_match - I throw chain with guin in php - to validate telephone with php - regular expression date - regular expressions mails php - movable regular expression - expresion regular validar email - validation of telephone in php - regular expressions PHP date - string - to validate telephone php - to verify telephone php - expression to validate hour asp - regular and mail expression php - regular expression to validate the mail - to validate telephone regular expression php - regular expression I number PHP - regular expressions to verify mail PHP - to validate telephone numbers php - to validate hour with php - php to validate URL regular expression - regular expression for email in php - Validation of fechas php - to verify with php - php examples regular Expressions - php to validate URL - to validate URL with expressions - regular expression for an electronic mail - validador of regular expressions for php - to validate email php regular expression - validador United States telephone Javascript regular expressions - list regular expressions PHP - to validate string php -