PHP: Simple REGEX Quick Reference for use with preg_match Function CodeUnit 30 NOV 2011

As always, checking to see if one string is contained within another will always be faster using the strpos or strstr functions. However, for more complex checks, REGEX (regular expressions) are the ticket, and with functions like PHP’s preg_match, there really isn’t any excuse for not doing proper value validation.

Anyway, just as a note to myself, here is a quick reference sheet for some basic regex:

  • [abc] – A single character: a, b or c
  • [^abc] – Any single character but a, b, or c
  • [a-z] – Any single character in the range a-z
  • [a-zA-Z] – Any single character in the range a-z or A-Z
  • ^ – Start of line
  • $ – End of line
  • A – Start of string
  • z – End of string
  • . – Any single character
  • s – Any whitespace character
  • S – Any non-whitespace character
  • d – Any digit
  • D – Any non-digit
  • w – Any word character (letter, number, underscore)
  • W – Any non-word character
  • b – Any word boundary character
  • (…) – Capture everything enclosed
  • (a|b) – a or b
  • a? – Zero or one of a
  • a* – Zero or more of a
  • a+ – One or more of a
  • a{3} – Exactly 3 of a
  • a{3,} – 3 or more of a
  • a{3,6} – Between 3 and 6 of a

Additional options that can be specified in a regex string:

  • i – case insensitive
  • m – make
  • . – match newlines
  • x – ignore whitespace in regex
  • o – perform #{…} substitutions only once

Some examples (also) lifted from the PHP site:

Example #1 Find the string of text “php”

// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

Example #2 Find the word “web”

/* The b in the pattern indicates a word boundary, so only the distinct
 * word "web" is matched, and not a word partial like "webbing" or "cobweb" */
if (preg_match("/bwebb/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

if (preg_match("/bwebb/i", "PHP is the website scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

Example #3 Getting the domain name out of a URL

// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i',
    "http://www.php.net/index.html", $matches);
$host = $matches[1];

// get last two segments of host name
preg_match('/[^.]+.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}n";

//The above example will output: domain name is: php.net

Nifty.

Related Posts:

About Craig Lotter

Software developer, husband and dad to two little girls. Writer behind An Exploring South African. I don't have time for myself any more.