00001 <?php
00002
00003
00004
00005
00006
00007
00008 if( isset( $_POST["text"] )) $text = $_POST["text"];
00009 else $text = "";
00010 if( isset( $_POST["regexp"])) $regexp = $_POST["regexp"];
00011 else $regexp = "";
00012 if( isset( $_POST["type"] )) $type = $_POST["type"];
00013 else $type = "";
00014 $resultsAvailable = false;
00015 $successful = false;
00016
00017
00018 if( $text != "" && $regexp != "" && ( $type == "preg_match" || $type == "preg_match_all")) {
00019 if( $type == "preg_match" ) {
00020
00021 $result = preg_match( $regexp, $text, $matches );
00022 if( isset( $matches[1] ))
00023 $matchStrings = Array( $matches[1] );
00024 else
00025 $matchStrings = Array();
00026 }
00027 else {
00028 $result = preg_match_all( $regexp, $text, $matches );
00029 $matchStrings = $matches[1];
00030 }
00031
00032
00033 if( $result ) {
00034 $resultsAvailable = true;
00035 $successful = true;
00036 }
00037 else {
00038 $resultsAvailable = true;
00039 $successful = false;
00040 $message = "The regular expression did not match.";
00041 }
00042 }
00043 else {
00044
00045 }
00046 ?>
00047 <html>
00048 <head>
00049 <title>Interactive Regular Expressions!</title>
00050 <script type="text/javascript">
00051 function clearForm()
00052 {
00053 text = document.getElementById('text');
00054 text.value = '';
00055 regexp = document.getElementById('regexp');
00056 regexp.value = '';
00057 }
00058 </script>
00059 </head>
00060 <body>
00061 <h2>Regular Expression Testing Tool</h2>
00062 <?php
00063 if( $resultsAvailable ) {
00064 if( $successful ) {
00065 $numMatches = count($matchStrings);
00066 print("<p>Number of matches: <b>$numMatches</b></p>");
00067 print("<ol>");
00068 foreach( $matchStrings as $match ) {
00069 print("<li>$match</li>");
00070 }
00071 print("</ol>");
00072 print_r($matches);
00073 }
00074 else {
00075 print( $message );
00076 }
00077 print("<hr/>");
00078 }
00079 ?>
00080 <form name="regexp" method="post">
00081 <label for="regexp">Type your regular expression:</label>
00082 <br/>
00083 <textarea name="regexp" id="regexp" cols="50" rows="5"><?php echo $regexp; ?></textarea>
00084 <br/>
00085 <label for="text">Type the test text:</label>
00086 <br/>
00087 <textarea name="text" id="text" cols="50" rows="5"><?php echo $text; ?></textarea>
00088 <br/>
00089 <label for="type">Choose a method:</label>
00090 <select name="type">
00091 <option value="preg_match" <?php if( $type=="preg_match" ) { ?>selected="selected"<?php } ?>>preg_match</option>
00092 <option value="preg_match_all" <?php if( $type=="preg_match_all" ) { ?>selected="selected"<?php } ?>>preg_match_all</option>
00093 </select>
00094 <input type="submit" name="Submit" value="Test!" />
00095 <input type="button" name="Clear" onClick="javascript:clearForm();" value="Clear Form" />
00096 </form>
00097 </body>
00098 </html>
00099