That didn't match? I've begun to grasp regex to some extent though the things I just tried made me realize there is plenty of logic I'm missing thus far.
Here is the full file I'm using to test regex filters that is making it very easy for me to test the regex filters out...
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>PHP Serverside Validation of CSS post data</title>
<style type="text/css">
body,html {font-family: monospace;}
b {color: #00f;}
b.bad {color: #f00;}
b.good {color: #0f0;}
div {outline: #f00 solid 1px;}
div.overflow {float: left; font-size: 12px; height: 240px; margin: 4px; overflow: auto; width: 25%;}
form {float: left; width: 400px;}
form input {font-size: 10px; width: 60px;}
</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset>
<div>
<input name="ce000" value="body" />
<input name="ce001" value="444" />
<input name="ce002" value="11/002" />
<input name="ce003" value="00f" />
<input name="ce004" value="00f" />
</div>
<div>
<input name="ce010" value="h2, h2, h3, h4" />
<input name="ce011" value="transparent" />
<input name="ce012" value="none" />
<input name="ce013" value="1f1" />
<input name="ce014" value="115" />
</div>
<div>
<input name="ce020" value="div" />
<input name="ce021" value="55555" />
<input name="ce022" value="01/002" />
<input name="ce023" value="5f6" />
<input name="ce024" value="5f6" />
</div>
<div>
<input name="ce030" value="span" />
<input name="ce031" value="7d6" />
<input name="ce032" value="" />
<input name="ce033" value="" />
<input name="ce034" value="" />
</div>
<div>
<input name="ce040" value="" />
<input name="ce041" value="" />
<input name="ce042" value="" />
<input name="ce043" value="" />
<input name="ce044" value="" />
</div>
<div>
<input name="ce050" value="" />
<input name="ce051" value="" />
<input name="ce052" value="" />
<input name="ce053" value="" />
<input name="ce054" value="" />
</div>
<div>
<input name="ce060" value="" />
<input name="ce061" value="" />
<input name="ce062" value="" />
<input name="ce063" value="" />
<input name="ce064" value="" />
</div>
<div>
<input name="ce070" value="" />
<input name="ce071" value="" />
<input name="ce072" value="" />
<input name="ce073" value="" />
<input name="ce074" value="" />
</div>
<div>
<input name="ce080" value="" />
<input name="ce081" value="" />
<input name="ce082" value="" />
<input name="ce083" value="" />
<input name="ce084" value="" />
</div>
<br style="clear: both;" />
<input style="display: block; width: 60%;" type="submit" value="Validate Form Data" />
</fieldset>
</form>
<?php
function validate_clientside_array($regex, $position)
{
$item = '/'.$position.'$/';
echo 'parameter 1 = <b>'.$regex.'</b><br />';
echo 'parameter 3 = <b>'.$position.'</b><br /><br />';
foreach($_POST as $key => $value)
if (preg_match($item, $key))
{
if ($validity != '1')
{
if (preg_match($regex, $value)) {echo ' <b class="good">'.$key.' = '.$value.'</b> is a <b class="good">match</b>!</b><br />'."\n";}
else {echo '<b class="bad">'.$key.' == '.$value.'</b> not a <b class="bad">match</b>!</b><br />'; $validity = '1';}
/*return false;*/
}
}
}
$regex_0_selectors = '/[0-9a-z]((.|,|:)[0-9a-z]){0,10}/'; //'/[0-9a-z]((.|,|:)[0-9a-z]){0,10}/';
$regex_1_colors = '/^(?:(?:[0-9a-f]{3}){1,2}|transparent)?$/';
$regex_2_bgimages = '/^(([0-9]{2})\/([0-9]{3})|none)?$/';
echo '<div class="overflow"><h3>Column 0 - Selectors</h3>';
validate_clientside_array($regex_0_selectors,'0');
echo '</div>';
echo '<div class="overflow"><h3>Column 2 - Background-images</h3>';
validate_clientside_array($regex_2_bgimages,'2');
echo '</div><br style="clear: both;" />';
echo '<div class="overflow"><h3>Column 1 - Hex Colors</h3>';
validate_clientside_array($regex_1_colors,'1');
echo '</div>';
echo '<div class="overflow"><h3>Column 3 - Hex Colors</h3>';
validate_clientside_array($regex_1_colors,'3');
echo '</div>';
echo '<div class="overflow"><h3>Column 4 - Hex Colors</h3>';
validate_clientside_array($regex_1_colors,'4');
echo '</div><br style="clear: both;" />';
echo $validity.' <b><--- (if you don\'t see \'hi mom\' to the left of the arrow then script will falsely proclaim validation, DOH!)</b>';
if (isset($validity)) {echo '<br />The array <b class="bad">contains invalid data</b>!';}
else {echo '<br />The array contains <b class="good">only valid data</b>.';}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$comma_separated = implode("?", $_POST);
echo '<p><b>Imploded Post Data:</b> '.$comma_separated.'</p>'."\n";
}
?>
<div>
<p>Valid data includes 'transparent', short-hand hexidecimal (fff, 0ff, 123, etc), and regular hexidecimal values.</p>
<p>Any other data <b><i>should</i></b> trigger an error and in which case all the form data should be rejected as a whole!</p>
</div>
</body>
</html>
Bookmarks