Regular expression checker
| From: | memoimyself at yahoo dot com dot br | Date: | Wed, 17 Mar 2004 19:30:22 +0000 |
| Subject: | Regular expression checker | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-180722@lists.php.net to get a copy of this message | ||
Hello all,
For a while I tried in vain to find a decent regular expression tester. The closest I found
was RegExpEditor, by the same people who produce PHPEdit, but I found it to be slow
and didn't like its constant bitching about "invalid delimiters" or some such thing.
Anyway, I ended up writing a little tester for Perl-compatible regular expressions and
thought I'd share it with you. Maybe it'll come in handy to people who often need to use
regular expressions in their projects. Just copy the code below the dotted line (which
has Unix line breaks) and save it to a file.
Cheers,
Erik
--------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Regular Expression Tester</title>
<?php( isset($_POST['text']) && isset($_POST['regexp']) )
{
$error = false;
if($_POST['mode'] == 'one')
{
if( @preg_match('/'.$_POST['regexp'].'/', $_POST['text'],
$matches) ===
false )
{
$error = true;
}
}
else
{
if( @preg_match_all('/'.$_POST['regexp'].'/',
$_POST['text'], $matches)
=== false )
{
$error = true;
}
}
}
?>
</head>
<body style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;">
<form action="<?php print $_SERVER['PHP_SELF']; ?>"
method="post"
enctype="multipart/form-data">
<table width="750">
<tr>
<td width="135"><div align="right">Regular
expression:</div></td>
<td width="591">
<input name="regexp" type="text" size="87"
<?php if( isset($_POST['regexp']) ) {print
'value="'.$_POST['regexp'].'"';} ?>
/>
</td>
</tr>
<tr>
<td width="135"><div align="right">Text:</div></td>
<td width="591">
<textarea name="text" rows="6" cols="65">
<?php if( isset($_POST['text']) ) {print $_POST['text'];} ?>
</textarea>
</td>
</tr>
<tr>
<td width="135"><div align="right">Mode:</div></td>
<td width="591">
<select name="mode">
<option value="one"
<?php( isset($_POST['mode']) && $_POST['mode'] == 'one' )
{
print 'selected';
}
?>
>First match</option>
<option value="all"
<?php( isset($_POST['mode']) && $_POST['mode'] == 'all' )
{
print 'selected';
}
?>
>All matches</option>
</select>
</td>
</tr>
<tr>
<td width="135"> </td>
<td> <input type="submit" value="RUN TEST"/>
<input type="reset" value="CLEAR FORM"/></td>
</tr>
</table>
</form>
<p/>
<div style="background-color:#000000; color:#FFFFFF;">
<table width="750" border="10" bordercolor="#000000">
<?php($error)
{
exit('<tr><td><span style="color: lime; font-weight:
bold;">'.
'Invalid regular expression!</span></td></tr>');
}
( isset($matches) && !empty($matches[0]) )
{
print '<tr><td colspan="2" style="font-weight:bold;">Full
match(es):</td></tr>'.
'<tr><td colspan="2"> </td></tr>';
if( !is_array($matches[0]) )
{
print '<tr>'.
'<td width="25" style="font-weight:
bold;">1</td>'."\n".
'<td>'.htmlentities($matches[0]).'</td>'.
'<tr/>';
}
else
{
foreach($matches[0] as $key => $val)
{
print '<td width="25" style="font-weight: bold;">'.($key+1).
'</td>'."\n".'<td>'.htmlentities($val).'</td>'."\n".
'</tr>';
}
}
}
{
print '<tr><td><span style="color: lime; font-weight:
bold;">'.
'No matches found.</span></td></tr>';
}
( isset($matches[1]) && !empty($matches[1]) )
{
print '<tr width="100%"><td
colspan="2"> </td></tr>'.
'<tr width="100%"><td
colspan="2"><hr/></td></tr>'.
'<tr width="100%"><td
colspan="2"> </td></tr>'.
'<tr width="100%"><td colspan="2"
style="font-weight:bold;">'.
'Sub-pattern match(es):</td></tr>'.
'<tr width="100%"><td
colspan="2"> </td></tr>';
for($i = 1; $i < count($matches); $i++)
{
if( !is_array($matches[$i]) )
{
print '<tr width="100%">'.
'<td width="25" style="font-weight:
bold;">'.$i.'</td>'."\n".
'<td>'.htmlentities($matches[$i]).'</td>'.
'</tr>';
}
else
{
foreach($matches[$i] as $key => $val)
{
print '<tr width="100%">'.
'<td width="25" style="font-weight:
bold;">'.$i.'.'.($key+1).
'</td>'."\n".'<td>'.htmlentities($val).'</td>'."\n".
'</tr>';
}
}
if( isset($matches[$i+1]) )
{
print '<tr width="100%"><td
colspan="2"> </td></tr>';
}
}
}
?>
</table>
</div>
</body>
</html>