Skip to content

Instantly share code, notes, and snippets.

@rugk
Last active August 29, 2015 14:27
Show Gist options
  • Save rugk/3ce33ef42df17cefac28 to your computer and use it in GitHub Desktop.
Save rugk/3ce33ef42df17cefac28 to your computer and use it in GitHub Desktop.
PHP regular expression test script
<!DOCTYPE HTML>
<!-- original: https://gist.github.com/rugk/3ce33ef42df17cefac28 -->
<style>
pre {
background-color: #e4dc97;
overflow: scroll;
}
</style>
<?php
echo 'PHP version: ' . PHP_VERSION;
$outputfile='output.txt';
// Replace:
$string='';
if ($string == '') {
$string=file_get_contents('string.txt');
}
// through:
$pattern='';
if ($pattern == '') {
$pattern=file_get_contents('pattern.txt');
}
// with:
$replacement='';
if ($replacement == '') {
$replacement=file_get_contents('replacement.txt');
}
$test=preg_replace('/' . $pattern . '/', $replacement, $string);
echo '<h2>before</h2><pre>' . htmlspecialchars($string) . '</pre><hr>';
echo '<h2>pattern</h2><pre>' . htmlspecialchars($pattern) . '</pre>';
echo '<h2>replace with</h2><pre>' . htmlspecialchars($replacement) . '</pre><hr>';
echo '<h2>after</h2><pre>' . htmlspecialchars($test) . '</pre>';
if (isset($outputfile) && $outputfile != '') file_put_contents($outputfile, $test);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment