PHP Preg Match Script to Test Conditions


Get to know preg match in PHP. This script will test preg match conditions. It will take time for you just to write down a script. So I wrote one for you:

<html>
<head>
<title>PHP preg_match test online</title>
</head>
<body>
<?php
echo '<h3>Instructions:</h3>';
echo '<br />';
echo '<br />';
if (!isset($_GET['submitpregmatch'])){
//form is not submitted, show form
echo 'Description: This tool will test any pregmatch condition using PHP';
echo '<FORM action="'.$_SERVER['PHP_SELF'].'" method="get">';
echo 'Step 1: Enter pregmatch condition to test,example(in violet): <font color="violet">/^[-a-z._@,\'\s]*$/i</font><br />';
echo '<input type="text" name="submitpregmatch" size="100">';
echo '<input type="submit" value="Submit this!">';
echo '</form>';
echo '<br />';
}
else {
//pregmatch condition submitted show form
$submitpregmatch =trim(stripslashes($_GET['submitpregmatch']));
echo 'Step 2:This form will validate the pregmatch condition you just submitted (in green):<font color="green">'.$submitpregmatch.'</font>';
echo '<br />';
echo 'To change pregmatch criteria, simply reset this form, back to default.';
echo '<br></br>';
echo 'PHP will then automatically validate if the string you entered is ACCEPTABLE or INVALID based on the criteria upon form submission.';
echo '<br />';
echo '<br /><br />';
      if (!isset($_GET['test'])) {
      //Still not set, show form
      echo '<FORM action="'.$_SERVER['PHP_SELF'].'" method="get">';
      echo '<input type="hidden" name="submitpregmatch" value="'.$submitpregmatch.'">';
      echo 'Enter the string to be test: <input type="text" name="test" size="100">';
      echo '<input type="submit" value="Test this!">';
      echo '</form>';
      }
      else {
      //form is set, process input
      $test =$_GET['test'];
      $submitpregmatch =trim(stripslashes($_GET['submitpregmatch']));
      if (preg_match($submitpregmatch,$test))
      {
      echo 'Step 3<font color="blue"> THIS STRING: '.''.$test.''.' DOES MATCH WITH YOUR CONDITION -- ACCEPTED</font>';
      }
      else
      {
      echo 'Step 3<font color="red"> THIS STRING: '.''.$test.''.' DOES NOT MATCH WITH YOUR CONDITION -- INVALID</font>';
      }
      echo '<br></br>';
      echo 'Enter another test again';
      }
}
?>
<br />
<br />
</body>
</html>

You can see it in action here: PHP preg_match test online. Share and Enjoy..Thanks.



Related posts: