Matching a Pattern

You want to find a pattern (regular expression) within a string of text.

Use preg_match() and preg_match_all(). The difference between these two functions is that preg_match() stops searching after the first match, whereas preg_match_all() will continue searching until the end of the string, saving what it finds in an array.

<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" value="/world/" /><br />
Text:<br />
<input type="text" name="text" size="70" value="Welcome to the world of PHP. The Internet world is full of challenges." /><br />
<input type="submit" name="submit" value="Check" />
</form>
	<table border>
	<tr bgcolor="lightcyan">
	<th align="left">Regular Expression</th>
	<th align="left">Text</th>
	</tr>
	<tr>
	<td>/love/</td>
	<td>My lovely gloves are worse for wear, Love.
	</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($pattern, $text, $matches)) {
		echo "$pattern was matched. <br />";
	}
	else {
		echo "$pattern was not matched. <br />";
	}
	echo "<br />The matches array contains:<br />";
	foreach ($matches as $key =>$values) {
		echo $key, " => ", $values, "<br />";
	}
	echo "<br /><a href=\"$_SERVER[SCRIPT_NAME]\">Check again?</a>";
}
?>
</p>
</body>
</html>

View the effect

The preg_match_all() creates an array of all the patterns matched in the string, not just the first one, and returns the number of times it matched the pattern.

<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" value="/world/" /><br />
Text:<br />
<input type="text" name="text" size="70"
value="Welcome to the world of PHP. The Internet world is full of challenges." /><br />
<input type="submit" name="submit" value="Check" />
</form>
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