Skip to content

Instantly share code, notes, and snippets.

@PeeHaa
Created August 8, 2013 10:12
Show Gist options
  • Save PeeHaa/6183426 to your computer and use it in GitHub Desktop.
Save PeeHaa/6183426 to your computer and use it in GitHub Desktop.
Emailaddress regex pattern tester
<?php
if (isset($_POST['submit'])) {
$html = file_get_contents('http://fightingforalostcause.net/misc/2006/compare-email-regex.php');
$dom = new DOMDocument();
@$dom->loadHTML($html);
//$results = $dom->getElementById('results');
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//table[@id='results']/tr/td[1]");
$addresses = [
'valid' => [],
'invalid' => [],
];
$type = 'valid';
foreach ($nodes as $node) {
if ($node->textContent == 'These should be invalid') {
$type = 'invalid';
continue;
} elseif ($node->textContent == 'These should be valid' || !trim($node->textContent)) {
continue;
}
$addresses[$type][] = $node->textContent;
}
echo '<h1>Valid emailaddresses</h1>';
echo '<table>';
foreach ($addresses['valid'] as $address) {
$passFail = (preg_match($_POST['pattern'], $address) === 1) ? 'PASS' : 'FAIL';
echo ' <tr>';
echo ' <td>' . $address . '</td>';
echo ' <td>' . $passFail . '</td>';
echo ' </tr>';
}
echo '</table>';
echo '<h1>Invalid emailaddresses</h1>';
echo '<table>';
foreach ($addresses['valid'] as $address) {
$passFail = (preg_match($_POST['pattern'], $address) === 0) ? 'PASS' : 'FAIL';
echo ' <tr>';
echo ' <td>' . $address . '</td>';
echo ' <td>' . $passFail . '</td>';
echo ' </tr>';
}
echo '</table>';
}
?>
<form action="" method="post">
<input type="text" name="pattern">
<input type="submit" name="submit" value="validate">
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment