Basics of the PHP PCRE functions

I'm only going to talk about the three I use the most but, there are a number of useful PCRE functions in PHP.


int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] )

preg_match returns 0 for no match or 1 if a match was found. It's important to note that preg_match stops searching after the first match. Use [preg_match_all][] if you need all matches within a string.

You can get back any backreferences by passing an empty array as the $matches argument.

Here's a simple example that we use to parse a string like 'variable = value' into pieces:

~~~~ {.php name="code"} $regex = '/^\s*(.*?)\s*=\s*(.*?)\s*$/';$subject = 'variable = value';$matches = array();if( preg_match($regex, $subject, $matches) ) { echo '

' . print_r($matches, true) . '
';} wzxhzdk:0


mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit= -1 [, int &$count ]] )

Most of the time [str_replace][] is sufficient (and faster!) but preg_replace has some great uses as well. In its most basic form, you pass it a regular expression and a replacement value and it replaces all matches.

Using the regex and subject in the previous example, let's see how we can use preg_replace to reverse the values on either side of the =. If we start with 'variable = value', we want to end up with 'value = variable'.

~~~~ {.php name="code"} $regex = '/^\s*(.*?)\s*=\s*(.*?)\s*$/';$subject = 'variable = value';echo preg_replace($regex, '$2 = $1', $subject); wzxhzdk:1

which outputs this

Array( [0] => The [1] => quick [2] => brown [3] => fox [4] => jumped [5] => over [6] => the [7] => lazy [8] => dog [9] => )

If you're looking for tutorials and documentation on lots of different flavors of regular expressions, I highly recommend