Pattern Modifiers

A modifier allows you to control the way a pattern match is handled. For example, if you want to search for a pattern and turn off the case sensitivity, you can use the i modifier: /pattern/i.

Character Meaning
A Matches only the beginning of a string even if newlines are embedded and the m modifier is used.
D Matches only at the end of the string. Without this modifier, a dollar sign is ignored if the m modifier is set.
e When performing replacements with preg_replace(), the replacement side is evaluated as an expression.
i Turn off case sensitivity.
m If a string has embedded newlines, each newline within the string marks the end of that string. The beginning and end of line metacharacters (^ and $) apply to each of the nested strings rather than to the entire string.
S Studying a pattern if it is used often to optimize the search time.
s Allows the dot metacharacter to match on any newlines within a string. Normally the does not match on the newline character.
X Any backlash in a pattern folowed by a letter that has no special meaning causes an error.
x Ignores whitespace in the pattern except when escaped with a backslash or within brackets; good for commenting regualr expressions to make them easier to read.
U Turns off the default "greediness" of the quantifiers, but greediness can be temporarily turned on if the u is followed by a question mark.

<html>
<head>
<title>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
if (isset($_POST['submit'])) {
		process_form();
	}
	else {
		display_form();// display form for the first time
	}

function display_form() {
	echo <<<HTML
	<form action = "$_SERVER[SCRIPT_NAME]" method="post">
	Regular Expression Pattern (include the delimiter):<br />
	<input type="text" name="pattern" size="70"
	value="/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}\s?$/m" /><br />
	<font color="blue">(use \s? to take care of whitespace like carriage returns
	with 0 or 1 occurence. m is for multiple lines)</font>
	<br />
	Text:<br />
	<textarea name="text" rows="5" cols="50">
	myname@email.com
	john.smith123@email.org
	Welcome to PHP
	john_doe@email.co.uk
	i-love@mail.travel
	</textarea><br />
	<input type="submit" name="submit" value="Check" />
	</form>
	<table border>
	<tr bgcolor="lightcyan">
	<th align="left">Type of Input</th>
	<th align="left">Regular Expression</th>
	</tr>
	<tr>
	<td>Zip Code</td>
	<td><b>/^(\d{5})(-\d{4})?$/</b>
	<br />(^(\d{5}) starts with 5 digits, (-\d{4})?$ means dash with 4 digits at the end.
	? means 0 or 1 as zip code can have 5 or 9 digits (last 4 are optional))

	<br />or<br />
	<b>/^\d{5}((-|\s)?\d{4})?$/</b>
	</td>
	</tr>
	<tr bgcolor="lightyellow">
	<td>E-mail</td>
	<td><b>/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/</b>
	<br />(^[\w.-]+ starts with 1 or more letters, numbers, underscore, period and a dash.
	\.[A-Za-z]{2,6} ends with one period and between 2 and 6 letters to account for .com, .travel, etc.)
	<br />or<br />
	<b>/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6})$/</b>
	</td>
	</tr>
	<tr>
	<td>U.S. phone number</td>
	<td><b>/^\(?\d{3}\)?-?\s*\d{3}\s*-?\d{4}$/</b>
	</td>
	</tr>
	<tr bgcolor="lightyellow">
	<td>Credit card number</td>
	<td><b>/^((4\d{3})|(5[1-5]\d{2})|(6011))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}$/</b>
	</td>
	</tr>
	<tr>
	<td>Social Security number</td>
	<td><b>/^\d{3}-?\d\d-?\d{4}$/</b>
	</td>
	</tr>
	<tr bgcolor="lightyellow">
	<td>URL</td>
	<td><b>/^((http\https|ftp)://)?([\w-])+(\.)(\w){2,4}([\w/+=%&_.~?-]*)$/</b>
	</td>
	</tr>
	</table>
HTML;
}

function process_form() {
	// stripslashes and trim the strings
	$pattern = stripslashes(trim($_POST['pattern']));
	$text = trim($_POST['text']);
	echo "The result of checking <br /><font color=red>$pattern</font><br />
	against<br /><font color=red>$text</font><br /> is:<br />";
	// match the pattern
	if (preg_match_all($pattern, $text, $matches)) {
		echo "$pattern was matched. <br />";
	}
	else {
		echo "$pattern was not matched. <br />";
	}
	echo "<br />The matches array contains:<br />";
	// The matches are stored in the first element of the array
	foreach ($matches[0] as $key =>$values) {
		echo $key, " => ", $values, "<br />";
	}
	echo "<br /><a href=\"$_SERVER[SCRIPT_NAME]\">Check again?</a>";

}
?>
</p>
</body>
</html>

View the effect


Regular Expressions | Introduction | Matching a Pattern | Metacharacters | Quantifiers | Character Classes | Pattern Modifiers | Matching and Replacing Patterns | Splitting Strings | Finding Elements of an Array | Finding the nth Occurence of a Match | Choosing Greedy or Nongreedy matches
© 2008: Hann So
email: hso@voyager.deanza.edu