PHP Cheat Sheet: Regular Expression

1. preg_match():

if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";

Note: $matches[0]will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

$str =;
if (preg_match('@^(?:http://)?([^/]+)@i', $str, $matches) {
    $host = $matches[1];

2. preg_match_all():

$str = "<b>example: </b><div align=left>this is a test</div>";
preg_match_all("|<[^>]+>(.*)</[^>]+>|U", $str, $out);
echo $out[0][0];  // "<b>example: </b>"
echo $out[0][1];  // "<div align=left>this is a test</div>"
echo $out[1][0];  // "example: "
echo $out[1][1];  // "this is a test"


$userinfo = "Name: <b>Joe Black</b> <br> Title: <b>PHP Guru</b>";
preg_match_all("/<b>(.*)<\/b>/U", $userinfo, $pat_array);
echo $pat_array[0][0];  // "Joe Black"
echo $pat_array[0][1];  // "PHP Guru"

3. preg_replace():

The preg_replace() function replaces all occurrences of pattern with replacement, and returns the
modified result.

$text = "This is a link to";
echo preg_replace("/http:\/\/(.*)\//", "<a href=\"\${0}\">\${0}</a>", $text);
// "This is a link to
// <a href=""></a>."

If you pass arrays as the pattern and replacement parameters, the function will cycle through each
element of each array, making replacements as they are found.

$draft = "In 2010 the company faced plummeting revenues and scandal.";
$keywords = array("/faced/", "/plummeting/", "/scandal/");
$replacements = array("celebrated", "skyrocketing", "expansion");
echo preg_replace($keywords, $replacements, $draft);
// "In 2010 the company celebrated skyrocketing revenues and expansion."

4. Regular Expression Modifiers

  • i:     case-insensitive
  • m:     Treat a string as several (m for multiple) lines. By default, the ^ and $ characters match at the very start and very end of the string in question. Using the m modifier will allow for ^ and $ to match at the beginning of any line in a string.
  • s:    Let a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded.
  • U:    Turns off greety matching.

Read More