View Single Post
  #4 (permalink)  
Old December 13th, 2004, 09:07 AM
SiliconFuRy SiliconFuRy is offline
Authorized User
 
Join Date: Oct 2004
Location: Edinburgh, Scotland, United Kingdom.
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SiliconFuRy
Default

Finally got a piece of example code working, along with a little regex help from a friend of mine... heres what i got working...

<?php
$str = "name=james love\nage=20\nface=cake\nshoes=happy";
$matches = Array();
preg_match_all("/([a-z_][a-z0-9]*)=(.+?)/iU", $str,$matches);
// /([a-z_][a-z0-9]*)=(.+?)/iU
// /([^=]+)=(.+)/

print_r($matches);

$allowed_vars = Array("name","age","face");

foreach($matches[1] as $k=>$v) {
        if(!in_array($v,$allowed_vars)) {
                echo "Variable name: " .
                $v . " not allowed, skipping...\n";
                continue;
        }
        $$v = $matches[2][$k];
}

echo "name: " . $name;
echo "\n age: : " . $age;
echo "\n face: " . $face;

?>

this, as you see, also succesfully sanitizes the variables, so "shoes" isnt allowed and is not converted to a variable.

Thanks again

Many shoes,

Jamez/SiliconFuRy
Reply With Quote