Skip to content

Instantly share code, notes, and snippets.

@szbl
Created January 2, 2013 18:07
Show Gist options
  • Save szbl/4436547 to your computer and use it in GitHub Desktop.
Save szbl/4436547 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<title>PCRE TEST</title>
<style type="text/css">
body { font: 14px HelveticaNeue,Helvetica,Arial,Sans-serif; }
table { width: 80%; }
textarea.code,pre { width:80%;height:100px;margin:1em;padding:1em;border:1px solid #ccc;background:#f7f7f7;font-family:Monaco,Courier,monospace; overflow: scroll; }
#summary { margin: 1em; border-collapse: collapse; border: 1px solid #d7d7d7; }
#summary td { border: 1px solid #d7d7d7; padding: .5em; }
</style>
</head>
<body>
<?php
if ( isset( $_POST['pattern'] ) && isset( $_POST['string'] ) )
{
$delimiter = isset( $_POST['delim'] ) ? $_POST['delim'] : '#';
$string = get_magic_quotes_gpc() ? stripslashes( $_POST['string'] ) : $_POST['string'];
$pattern = get_magic_quotes_gpc() ? stripslashes( $_POST['pattern'] ) : $_POST['pattern'];
// $the_pattern = $delimiter . preg_quote( $pattern, $delimiter ) . $delimiter;
$the_pattern = $delimiter . $pattern . $delimiter;
if ( isset( $_POST['mods'] ) && is_array( $_POST['mods'] ) )
{
$the_pattern .= implode( $_POST['mods'] );
}
$matches = array();
$match = preg_match( $the_pattern, $string, $matches );
if ( isset( $_POST['replace'] ) )
{
$replace = get_magic_quotes_gpc() ? stripslashes( $_POST['replace'] ) : $_POST['replace'];
$the_replace_string = preg_replace( $the_pattern, $replace, $string );
}
if ( isset( $matches ) )
{
?>
<p><strong>Regex:</strong><br>
<pre class="code"><?php echo htmlentities( $the_pattern ); ?></pre></p>
<p><strong>Original String:</strong><br>
<pre class="code"><?php echo htmlentities( $string ); ?></pre></p>
<p><strong>Match?</strong><br>
<pre class="code"><?php var_dump( $match ); ?></pre></p>
<p><strong>Match Result</strong><br>
<pre class="code"><?php var_dump( $matches ); ?></pre></p>
<?php if ( isset( $the_replace_string ) ) : ?>
<p><strong>Replaced String:</strong><br>
<pre class="code"><?php echo htmlentities( $the_replace_string ); ?></pre></p>
<?php endif; ?>
<?php
}
}
?>
<form method="post" action="">
<table class="form-table">
<tbody>
<tr>
<th>
<label>Regex Delimeter</label>
</th>
<td>
<input type="text" name="delim" value="<?php echo isset( $delim ) ? htmlentities( $delim ) : '#'; ?>" maxsize="1" size="1">
</td>
</tr>
<tr>
<th>
<label>Pattern</label>
</th>
<td>
<input type="text" name="pattern" value="<?php echo isset( $pattern ) ? htmlentities( $pattern ) : ''; ?>">
</td>
</tr>
<tr>
<th>
<label>Replace</label>
</th>
<td>
<input type="text" name="replace" value="<?php echo isset( $replace ) ? htmlentities( $replace ) : ''; ?>">
</td>
</tr>
<tr>
<th>
<label>Modifiers</label>
</th>
<td>
<label for="mod-i"><input type="checkbox" id="mod-i" name="mods[]" value="i" checked="checked">i - case insensitive</label>
<br>
<label for="mod-m"><input type="checkbox" id="mod-m" name="mods[]" value="m" checked="checked">m - multiline</label>
</td>
</tr>
<tr>
<th>
<label>Test String</label>
</th>
<td>
<textarea name="string" class="code"><?php echo isset( $string ) ? htmlentities( $string ) : ''; ?></textarea>
</td>
</tr>
</tbody>
</table>
<p class="submit">
<input type="submit" value="Test">
</p>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment