Code: Select all
$regex_a = '/^[a-f0-9]{3}$/';Moderator: General Moderators
Code: Select all
$regex_a = '/^[a-f0-9]{3}$/';Code: Select all
$regex_a = '/^(([a-f0-9]{3})|([a-f0-9]{6}))$/';Code: Select all
$regex_a = '/^(([0-9a-f]{1,2}){3}|transparent|none)?$/';Your regex will also match 4 character long hexidecimal strings. I think you meant to do:JAB Creations wrote:Awesome! Thank you Scottayy! I'm totally starting to comprehend the regex anti-maze!
Code: Select all
$regex_a = '/^(([0-9a-f]{1,2}){3}|transparent|none)?$/';
Code: Select all
$regex_a = '/^(([0-9a-f]{1,3}){2}|transparent|none)?$/';That regex will match 4 character long hexadecimal strings as well. Actually, 2 char long hexadecimals will even be matched too.prometheuzz wrote:Your regex will also match 4 character long hexidecimal strings. I think you meant to do:
Code: Select all
$regex_a = '/^(([0-9a-f]{1,3}){2}|transparent|none)?$/';
Code: Select all
$regex_a = '/^(?:(?:[0-9a-f]{3}){1,2}|transparent|none)?$/';Doh! I didn't know what I was thinking!GeertDD wrote:...
That regex will match 4 character long hexadecimal strings as well. Actually, 2 char long hexadecimals will even be matched too.
This should be better:Code: Select all
$regex_a = '/^(?:(?:[0-9a-f]{3}){1,2}|transparent|none)?$/';
Code: Select all
<?php
$regex_a = '/^(([0-9a-f]{1,2}){3}|transparent|none)?$/';
function validate_array($regex, $var)
{
if ($validity != '1')
{
if (!preg_match($regex, $var)) {return false;}
}
return true;
}
foreach($_POST as $key => $value) {if(!validate_array($regex_a,$value)) {$validity = '1'; break;}}
if (!isset($validity))
{
$user_theme = implode(".", $_POST);
$compressed = gzcompress($user_theme, 9);
$var_12 = '24m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd89
24jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf
2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489
cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2
498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc42
89cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489j
c2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj824
89jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j8
9j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j24
89fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m8
9fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492
484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd24
98jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd
8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj24
89jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j
2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28
cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc
4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489
jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj824
89jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89
j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489
fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj
289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd49248
4224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd249
8jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd
8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj248
9jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2
489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj2
8cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489
jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc24
89jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj8
2489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890
j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j
2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224
m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd4
92484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924j
d2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf24
89jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj2498j2489c
j2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc4289cj28cj24
98j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2489jc428
9cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489jc2489jc2
489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j24fj82489
jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489fj890j89j
24fj82489jc2489jc2489jc4289cj28cj2498j2489cj2489jf2489jd8924jd2498jd492484224m89fj289j2489
fj890j89j24fj82489jc2489123114552345223542342342342343245423524545254244773575367356352
45245245245245243563461';
$var_23 = gzcompress($var_12, 9);
//setcookie('user_theme1',$user_theme,time()+2592000,'/');
//setcookie('user_theme2',$compressed,time()+2592000,'/');
/*
Cookie 4KB Limit = 4085 characters!!!!!!!!!!!!
*/
setcookie('user_theme3',$var_12,time()+2592000,'/');
setcookie('user_theme4',$var_23,time()+2592000,'/');
echo strlen($var_12).'<br /><br />';
echo strlen($var_23);
}
?><!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.overflow {height: 200px; overflow: auto; width: 40%;}
</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset>
<input name="ce000" value="000" />
<input name="ce001" value="fff" />
<input name="ce002" value="ff0" />
<input name="ce003" value="00f" />
<input name="ce010" value="a1b" />
<input name="ce011" value="7f6" />
<input name="ce012" value="transparent" />
<input name="ce013" value="1f" />
<input name="ce020" value="2f6" />
<input name="ce021" value="3f6" />
<input name="ce022" value="4f6" />
<input name="ce023" value="5f6" />
<input name="ce030" value="6f6" />
<input name="ce031" value="7d6" />
<input name="ce032" value="" />
<input name="ce033" value="" />
<input name="ce040" value="" />
<input name="ce041" value="" />
<input name="ce042" value="" />
<input name="ce043" value="" />
<input name="ce050" value="" />
<input name="ce051" value="" />
<input name="ce052" value="" />
<input name="ce053" value="" />
<input name="ce060" value="" />
<input name="ce061" value="" />
<input name="ce062" value="" />
<input name="ce063" value="" />
<input name="ce070" value="" />
<input name="ce071" value="" />
<input name="ce072" value="" />
<input name="ce073" value="" />
<input name="ce080" value="" />
<input name="ce081" value="" />
<input name="ce082" value="" />
<input name="ce083" value="" />
<input name="ce090" value="" />
<input name="ce091" value="" />
<input name="ce092" value="" />
<input name="ce093" value="" />
<br style="clear: both;" />
<!--
<label for="position" style="border: #000 dotted 1px; font-size: 18px;">Choose Regex Position:
<select id="position" name="position">
<option value="all">Display all form names and values.</option>
<option value="0">Name value ends with '0'.</option>
<option value="1">Name value ends with '1'.</option>
<option value="2">Name value ends with '2'.</option>
<option value="3">Name value ends with '3'.</option>
</select>
</label>
-->
<input style="display: block; width: 60%;" type="submit" value="Validate Form Data" />
</fieldset>
</form>
<div>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$comma_separated = implode(".", $_POST);
echo '<p><b>Imploded Post Data:</b> '.$comma_separated.'</p>'."\n";
}
echo '<div class="overflow">';
//$regex_a = '/^(([a-f0-9]{3})|([a-f0-9]{6}))$/';
$regex_a = '/^(([0-9a-f]{1,2}){3}|transparent|none)?$/';
$regex_b = '/^[a-f0-9]{6}$/';
function validate__clientside_array($regex, $var)
{
foreach($_POST as $key => $value)
if ($validity != '1')
{
if (preg_match($regex, $var)) {echo '<b class="good">'.$key.' = '.$var.'</b> is a <b class="good">match</b>!</b><br />'."\n";}
else {echo '<b class="bad">'.$var.'</b> not a <b class="bad">match</b>!</b><br />'; return false;}
}
return true;
}
if(!validate__clientside_array($regex_a,$value)) {$validity = '1';}
echo '</div>';
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>.';}
echo 'hi mom<br />';
?>
<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>Code: Select all
<!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.overflow {height: 200px; overflow: auto; width: 40%;}
form {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="fz" />
<input name="ce002" value="ff0" />
<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="1f" />
<input name="ce014" value="1f" />
</div>
<div>
<input name="ce020" value="div" />
<input name="ce021" value="3f6" />
<input name="ce022" value="4f6" />
<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;" />
<!--
<label for="position" style="border: #000 dotted 1px; font-size: 18px;">Choose Regex Position:
<select id="position" name="position">
<option value="all">Display all form names and values.</option>
<option value="0">Name value ends with '0'.</option>
<option value="1">Name value ends with '1'.</option>
<option value="2">Name value ends with '2'.</option>
<option value="3">Name value ends with '3'.</option>
</select>
</label>
-->
<input style="display: block; width: 60%;" type="submit" value="Validate Form Data" />
</fieldset>
</form>
<div>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$comma_separated = implode("?", $_POST);
echo '<p><b>Imploded Post Data:</b> '.$comma_separated.'</p>'."\n";
}
echo '<div class="overflow">';
//$regex_a = '/^(([a-f0-9]{3})|([a-f0-9]{6}))$/';
function validate_clientside_array($regex, $var, $position)
{
$item = '/'.$position.'$/';
$regex_0_selectors = '/([0-9a-z])/';
$regex_1_colors = '/^(([0-9a-f]{1,2}){3}|transparent)?$/';
$regex_1_bgimages = '/^(([0-9a-f]{1,2}){3}|none)?$/';
echo 'parameter 1 = '.$regex.'<br />';
echo 'parameter 2 = '.$var.'<br />';
echo 'parameter 3 = '.$position.'<br /><br />';
foreach($_POST as $key => $value)
if (preg_match($item, $key))
{
//{echo $key."=".$value.'<br />'."\n";}
//if ($validity != '1' && $key != 'ce002')
//echo '<br /><br />666 '.$regex.' 777<br /><br />';
if (preg_match($regex, $var)) {echo '<b class="good">'.$key.' = '.$value.'</b> is a <b class="good">match</b>!</b><br />'."\n";}
else {echo '<b class="bad">'.$key.' == '.$var.'</b> not a <b class="bad">match</b>!</b><br />';} // return false;}
}
return true;
}
// if (preg_match("/^abc/", $string)
$regex_0_selectors = '/([0-9a-z])/';
$regex_1_colors = '/^(([0-9a-f]{1,2}){3}|transparent|none)?$/';// '/^(?:(?:[0-9a-f]{3}){1,2}|transparent|none)?$/'; //'/^(([0-9a-f]{1,2}){3}|transparent)?$/';
$regex_1_bgimages = '/^(([0-9a-f]{1,2}){3}|none)?$/';
//validate_clientside_array('regex_0_selectors',$value,'0');
validate_clientside_array($regex_1_colors,$value,'1');
//validate_clientside_array($regex_1_colors,$value,'3');
//validate_clientside_array($regex_1_colors,$value,'4');
//if(!validate_clientside_array($regex_a,$value,'3')) {$validity = '1';}
echo '</div>';
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>.';}
?>
<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>Code: Select all
if (preg_match($regex, $var)) {echo '<b class="good">'.$key.' = '.$value.'</b> is a <b class="good">match</b>!</b><br />'."\n";}Code: Select all
if (preg_match($regex, $value)) {echo $value.' <b class="good">'.$key.' = '.$value.'</b> is a <b class="good">match</b>!</b><br />'."\n";}Code: Select all
<!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.overflow {float: left; font-size: 12px; height: 200px; overflow: auto; width: 25%;}
form {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="4444" />
<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;" />
<!--
<label for="position" style="border: #000 dotted 1px; font-size: 18px;">Choose Regex Position:
<select id="position" name="position">
<option value="all">Display all form names and values.</option>
<option value="0">Name value ends with '0'.</option>
<option value="1">Name value ends with '1'.</option>
<option value="2">Name value ends with '2'.</option>
<option value="3">Name value ends with '3'.</option>
</select>
</label>
-->
<input style="display: block; width: 60%;" type="submit" value="Validate Form Data" />
</fieldset>
</form>
<div>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$comma_separated = implode("?", $_POST);
echo '<p><b>Imploded Post Data:</b> '.$comma_separated.'</p>'."\n";
}
function validate_clientside_array($regex, $var, $position)
{
$item = '/'.$position.'$/';
$regex_0_selectors = '/([0-9a-z])/';
$regex_1_colors = '/^(?:(?:[0-9a-f]{3}){1,2}|transparent|none)?$/';
//$regex_1_colors = '/^(([0-9a-f]{1,2}){3}|transparent)?$/';
$regex_1_bgimages = '/^(([0-9a-f]{1,2}){3}|none)?$/';
echo 'parameter 1 = '.$regex.'<br />';
echo 'parameter 2 = '.$var.'<br />';
echo 'parameter 3 = <b>'.$position.'</b><br /><br />';
foreach($_POST as $key => $value)
if (preg_match($item, $key))
{
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 = 'hi mom';}/*return false;}*/
}
}
echo $validity.' <b><--- (if you don\'t see \'hi mom\' to the left of the arrow then script will falsely proclaim validation, DOH!)</b>';
$regex_0_selectors = '/([0-9a-z])/';
$regex_1_colors = '/^(([0-9a-f]{1,2}){3}|transparent|none)?$/';// '/^(?:(?:[0-9a-f]{3}){1,2}|transparent|none)?$/'; //'/^(([0-9a-f]{1,2}){3}|transparent)?$/';
$regex_1_bgimages = '/^(([0-9a-f]{1,2}){3}|none)?$/';
//validate_clientside_array('regex_0_selectors',$value,'0');
echo '<div class="overflow">';
validate_clientside_array($regex_1_colors,$value,'1');
echo '</div>';
echo '<div class="overflow">';
validate_clientside_array($regex_1_colors,$value,'3');
echo '</div>';
echo '<div class="overflow">';
validate_clientside_array($regex_1_colors,$value,'4');
echo '</div><br style="clear: both;" />';
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>.';}
?>
<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>Err, I don't think so. Run this:JAB Creations wrote:The regex filter on line 109 is still validating strings 4 or 5 characters in length.
...
Code: Select all
#!/usr/bin/php
<?php
$tests = array(
"1f",
"2b4",
"af12",
"abcfa",
"abc4fa",
"abc4fk",
"af12345"
);
$regex_1_colors = '/^(?:(?:[0-9a-f]{3}){1,2}|transparent|none)?$/';
foreach($tests as $t) {
if(preg_match($regex_1_colors, $t)) {
print "Yes, a match : $t\n";
} else {
print "No match : $t\n";
}
}
/* output:
No match : 1f
Yes, a match : 2b4
No match : af12
No match : abcfa
Yes, a match : abc4fa
No match : abc4fk
No match : af12345
*/
?>Code: Select all
$regex_0_selectors = '/([0-9a-z]).,([0-9a-z])/';Code: Select all
$regex_2_bgimages = '/^(([0-9]{2})\/([0-9]{3})|none)?$/';Code: Select all
<!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;" />
<!--
<label for="position" style="border: #000 dotted 1px; font-size: 18px;">Choose Regex Position:
<select id="position" name="position">
<option value="all">Display all form names and values.</option>
<option value="0">Name value ends with '0'.</option>
<option value="1">Name value ends with '1'.</option>
<option value="2">Name value ends with '2'.</option>
<option value="3">Name value ends with '3'.</option>
</select>
</label>
-->
<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])/';
$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_1_colors,'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>Code: Select all
$regex_0_selectors = '/([0-9a-z]).|,([0-9a-z])/';Code: Select all
<!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;" />
<!--
<label for="position" style="border: #000 dotted 1px; font-size: 18px;">Choose Regex Position:
<select id="position" name="position">
<option value="all">Display all form names and values.</option>
<option value="0">Name value ends with '0'.</option>
<option value="1">Name value ends with '1'.</option>
<option value="2">Name value ends with '2'.</option>
<option value="3">Name value ends with '3'.</option>
</select>
</label>
-->
<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])/';
$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>Couple of remarks: the regex-OR (the '|' meta character) has a very low precedence. In other words: "(ab)c|d(ef)" means: "abc OR def" and not "ab followed by a 'c' OR 'd' followed by ef".JAB Creations wrote:Two changes...
1.) I was validating the wrong regex code (my goof).
2.) I have created a new regex filter for selectors, 0-9, a-z, commas, and periods. The only problem is that blank selectors aren't yet optional. How do I amend my new regex filter to make blank values match?
Here is the new regex filter which in the main file below is located on line 118......Code: Select all
$regex_0_selectors = '/([0-9a-z]).|,([0-9a-z])/';
Code: Select all
$regex_0_selectors = '/[0-9a-z](\.|,)[0-9a-z]/';Code: Select all
/[0-9a-z]((.|,|:)[0-9a-z]){0,10}/Code: Select all
body h1 h1, h2, h3, h4 div div.border a:hover a:link a:focus