Simple Regular Expressions using preg_match()
Enter a string to be evaluated.
Enter a string to be evaluated.
<?php
include('includes/header.php');
error_reporting(E_ALL); // set error reporting to all
$self = basename($_SERVER['SCRIPT_NAME']);
$step = isset($_POST['step']) ? $_POST['step'] : 1;
$string = isset($_POST['string']) ? $_POST['string'] : null;
if ($step == 1) // first time into this script
{
?>
<h1>
Simple Regular Expressions using preg_match()
</h1>
<p>
Enter a string to be evaluated.
</p>
<form action="<?php echo($self); ?>" method="POST">
<fieldset>
<legend>Text:</legend>
<label for="string">String</label>
<input type="text" size="30" id="string" name="string" value="<?php echo($string); ?>">
</fieldset>
<input type="hidden" name="step" value="2">
<input type="submit" name="submit" value="Submit">
</form>
<?php
} // end of step 1
if ($step == 2) // second time into this script
{
// show results and provide a form to try again
// define arrays for search
$a = array();
$b = array();
$c = array();
$d = array();
$e = array();
$f = array();
// Note the preg_match pattern used to evaluate the string provided.
$aa = preg_match("/[A-z]/", $string, $a);
$bb = preg_match("/[A-Z]/", $string, $b);
$cc = preg_match("/[a-z]/", $string, $c);
$dd = preg_match("/[A-a]/", $string, $d);
$ee = preg_match("/tedd/", $string, $e);
$ff = preg_match("/world/", $string, $f);
?>
<h3> String Provided: <?php echo($string); ?></h3>
<p>
1. preg_match("/[A-z]/", <?php echo($string); ?>); = <?php if ($aa) {
print_r($a[0]);
} ?><br>
2. preg_match("/[A-Z]/", <?php echo($string); ?>); = <?php if ($bb) {
print_r($b[0]);
} ?><br>
3. preg_match("/[a-z]/", <?php echo($string); ?>); = <?php if ($cc) {
print_r($c[0]);
} ?><br>
4. preg_match("/[A-a]/", <?php echo($string); ?>); =<?php if ($dd) {
print_r($d[0]);
} ?><br>
5. preg_match("/tedd/", <?php echo($string); ?>); = <?php if ($ee) {
print_r($e[0]);
} ?><br>
6. preg_match("/world/", <?php echo($string); ?>); = <?php if ($ff) {
print_r($f[0]);
} ?><br>
</p>
<p>
Remember, these are the results of different preg_match() operations. <br>
Try different strings to see how different expressions work.
</p>
<p>
<a href="http://php.net/manual/en/function.preg-match.php">See the PHP manual</a>
</p>
<form action="<?php echo($self); ?>" method="POST">
<p>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Try Again">
</p>
</form>
<?php
} // end of step 2
include('includes/footer.php');
?>