This Online Regular Expression Tester with PHP makes use of PHP's "int ereg (string pattern, string string [, array & regs])" function. This ereg() function searches a string for matches to the regular expression given in pattern in a case-sensitive way. In other words, the ereg() function performs a regular expression match.
A regular expression is a string that describes a whole set of strings, according to certain syntax rules. These expressions are used by many text editors and utilities (especially in the Unix operating system) to search bodies of text for certain patterns and, for example, replace the found strings with a certain other string. Or in other words, a regular expression is for words what maths is for numbers .
First off. Place the following HTML text and PHP script in a file named regular-expression-tester.php and upload this file to your PHP server: (For a working online example, go to: http://programmabilities.com/php/regular-expression-tester.php.)
<html>
<head><title>Regular Expression Tester</title></head>
<body>
<h1>Regular Expression Tester</H1>
<form method="post" action="<?=$PHP_SELF;?>">
<font face="Courier">
String (for example: billgates@programmabilities.com):
<br/>
<input type="text" size="64" name="string"/>
<br/><br/>
Regular Expression:
<br/>
<input type="text" size="64" name="pattern" value="^([_a-zA-Z0-9-]+)@([a-zA-Z0-9-]+(.[a-zA-Z0-9-]+)*)$"/>
<br/><br/>
<br/>
<input type="submit"/>
</font>
</form>
<?php
echo "<br/><strong>string:</strong> $string";
echo "<br/><strong>regular expression:</strong> $pattern";
if(get_magic_quotes_gpc()) {
echo '<br/><br/>';
echo '<br/>Stripping magic quotes....';
$string = stripslashes($string);
$pattern = stripslashes($pattern);
echo "<br/><strong>string:</strong> $string";
echo "<br/><strong>regular expression:</strong> $pattern";
}
$found = ereg($pattern, $string, $matches);
echo '<br/><br/>';
if($found) {
echo '<br/><strong>valid:</strong> true';
echo '<br/><br/>';
echo '<br/><strong>Components: </strong>';
for($i=0; $i<count($matches); $i++)
{
echo "<br/>$matches[$i]";
}
} else {
echo '<br/><strong>valid:</strong> false';
}
?>
</body>
</html>
Notice the call of the function get_magic_quotes_gpc(). This function returns a value corresponding to the magic_quotes_gpc() PHP option, set by the PHP system administrator. If this option is set, PHP automatically adds backslashes to form variables so their values can be used in contexts that require protection of special characters. This feature is not helpful when entering, viewing, and using a regular expression. So, if the option is set, the script uses the stripslashes() function to remove any undesired slashes added automatically by PHP. To see why this is necessary, you can remove the if statement, and its body, from the script and try running the script. If the magic_quotes_gpc option is set in your PHP installation, the script will not operate properly.
Study the HTML page. Look at the regular expression specified as the default value of the textbox named pattern. This regular expression approximates the form of an e-mail address.
For a working example, go to: http://programmabilities.com/php/regular-expression-tester.php. Enter a value for the string and, if desired, replace the value of the regular expression, then click the Submit Query button.
When the script executes, it attempts to match the regular expression with a substring of the string and displays the result.
Chief Programmabilities
http://programmabilities.com