• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Regex input by users

I am trying to create a small system that allows someone to enter a regex (in an admin system) to then be used to validate input.

What I am looking for is two things really:

a) To validate the regex is valid and won't cause errors
b) To created the delimiters, ie escape the actual regex. So for example if using preg_match("/*./" .... then the correct way to escape based on / or any other options.

Any suggestions? Thanks!
0
thepadders
Asked:
thepadders
  • 3
  • 2
1 Solution
 
thepaddersAuthor Commented:
Also looking for the same sort of thing for replacement part for preg_replace ... if anyone has any ideas.
0
 
alextr2003frCommented:
try something like :
/* found at http://www.devpapers.com/article/238
 * few variables corrected
 */
<html>
 <head><title>Regular Expression Tester</title></head>
 <body>
  <h1>Regular Expression Tester</H1>

  <form method="post" action="<?php $_SERVER['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
 if (!empty($_POST)) {
        $string = $_POST['string'];
        $pattern = $_POST['pattern'];
        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>
0
 
thepaddersAuthor Commented:
Thank you, but this only runs a search and replace on a supplied regular expression. It does not validate the expression or or format it for use which was what I am looking for.
0
 
alextr2003frCommented:
you are welcome, you can handle errors on the $found result and put @ereg to not have error messages on the page
if you are useing php5 try { ... will do it easely.
0
 
alextr2003frCommented:
Hi I wrote this for you, it tests the validity of your reg. exp. and handles the delimiters too, I think it will be easy for you to tune it up for your needs :
<?php
  /* by Alex TR */
  function err_handler($errno, $errstr, $errfile, $errline){
    global $err,$err_descr;
    $err_descr = $errstr;
    $err = 1;
  }
  set_error_handler("err_handler");
  $output = '';
  if (!empty($_POST)) {
        $text = $_POST['text'];
        $pattern = $_POST['pattern'];
        if (get_magic_quotes_gpc()) {
           $string = stripslashes($string);
           $pattern = stripslashes($pattern);
        }
        $err = 0;
        $err_descr = '';
        $found = ereg($pattern,$text,$matches);
        if (!$err) {
                $output .= '<br><font color="green">your regexp is valid</font><br>';
                if (($found)) {
                        $output .= '<br><strong>Components: </strong>';
                        for ($i = 0; $i < count($matches); $i++) {
                                $output .= "<br/>$matches[$i]";
                        }
                } else {
                        $output .= '<br>nothing found<br>';
                }
        } else { $output .= '<br><font color="red">your regexp expression is invalid : '.$err_descr.'</font><br>';}
  } else {
        $pattern = '';
        $text = '';
  }
  echo '
  <h1 style="font: bold 14px Verdana">Regexp editor</H1>
  <form method="post" action="'.$_SERVER['PHP_SELF'].'">
   <font face="Verdana" size="1px">
    Text:
    <br>
    <textarea rows="10" name="text" style="width:100%">'.$text.'</textarea>
    <br><br>
    Regular Expression:
    <br>
    <input type="text" size="64" name="pattern" value="'.$pattern.'" style="width:100%">
    <br>'.$output.'
    <br>
    <input type="submit" value="submit">
   </font>
  </form>';
?>
Happy coding :-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now