Matching and Replacing Patterns

You want to match and replace patterns in a value.

Use preg_replace().

<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="/\b[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}\b/" /><br />
	<font color="blue">(use \b for word boundaries
	because the email can be within a text.)</font>
	<br />
	Replacement:<br />
	<input type="text" name="replace" size="70" value="<a href='mailto:$0'>$0</a>" /><br />
	<font color="blue">($0 refers to the entire match.)</font><br />
	Text:<br />
	<textarea name="text" rows="5" cols="50">
	Contact me at hann@mail.com
	</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']));
	$replace = stripslashes(trim($_POST['replace']));
	$replace2 = htmlentities(stripslashes(trim($_POST['replace'])));
	$text = trim($_POST['text']);
	echo "The result of replacing <br /><font color=red>$pattern</font><br />
	with<br /><font color=red>$replace2</font><br />
	in<br /><font color=red>$text</font><br /> is:<br />";
	// match the pattern first before doing the replacement
	if (preg_match($pattern, $text)) {
		echo preg_replace($pattern, $replace, $text), "<br />";
	}
	else {
		echo "$pattern was not matched. <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