PHP and Regular Expressions

preg_match()

The preg_match function is a php function used to determine if a string matches a particular regular expression. The syntax is simple: (copied from php.net)

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

The syntax is: preg_match(regularExpression, stringToMatch [, array matches]). In the example above, preg_match() looks for the string "php" in the text argument and returns a true or false.

preg_match will also poplulate an array called "matches" if a match is found. This contains the matched text.

preg_match_all()

The preg_match_all function is similar to the preg_match() function except that preg_match_all is global in scope. This means that is there are multiple matches is a string, preg_match_all will place them into the appropriate $matches array where as preg_match will only enter the first match.

A real world example of preg_match_all()

  //$d is the text from an html file
  preg_match_all("/<font size=\"\+2\"><a href=\"(.*?)\">(.*?)<\/font>/",$d,$matches);
  for($i=0; $i<count($matches[1]);$i++) {
    print "<a href=\"http://ssa.usps.gov/redir.php?url=http://www.usps.com/news/link/".$matches[1][$i]."\">".$matches[2][$i]."</a><br />";
  }

What this code does...

  1. First, I have an html file ($d)
  2. Next I run a preg_match_all against that file to look for all the links enclosed in <font size="+2> tags, and extract the target and text of those links.
  3. I then loop through my matches array and write links to my page.

preg_replace()

The preg_replace function has the following syntax:

preg_replace ( regularExpressions, replacements, strings )

For all three of these arguments, you can feed preg_replace arrays. A good example from the PHP.net site is below...

$string = "The quick brown fox jumped over the lazy dog.";

$patterns[0] = "/quick/";
$patterns[1] = "/brown/";
$patterns[2] = "/fox/";

$replacements[2] = "bear";
$replacements[1] = "black";
$replacements[0] = "slow";

echo preg_replace($patterns, $replacements, $string);

This will output...

The bear black slow jumped over the lazy dog.

Another example I've used is this...

$data = preg_replace("/(\r|\n|\t)*/","",$data);

This replaces all new lines, returns, and tabs with blanks. Essentially creating one long string from a multi-line file (such as an html document!)

NEXT
PREVIOUS
Master Index