All pastes #317030 Raw Edit

Anonymous

public php v1 · immutable
#317030 ·published 2007-01-15 06:33 UTC
rendered paste body
<?phpfunction conv_attr ($s){    if (!$len = strlen($s))         return false;    //explode(" ", $s) -- will broken at "with space value"    //explode("=", $s) -- broken at e="hello=world"        $start=false;    $keyfound=false;    for($i=0; $i<$len; $i++)    {        $c=$s[$i];        if (!$start && !$keyfound)        {            if ($c == "=")            {                echo "1";                $keyfound=true;                continue;            } else            {                $key.=$c;            }        }                 if ($keyfound)        {            if ($start === false)             {                $start="";                if ($c == '"' || $c == "'") $start=$c;                else $start="";                $value="";            } else            {                if ($c == $start || (!strlen($start) && $c==" "))                {                    $res[trim($key)]=$value;                    $start=false;$keyfound=false;                     $key=false;$value=false;                    echo "*";                } else                    $value.=$c;            }        }    }    echo "UnhandledKey[$key] Value[$value]\n";    return $res; }$attr='a="a.value" b="with space value" c=\'single quote\' d=without_quote e="hello=world" f = "another weird format"';/* expected result: $r=array(    'a'=>'a.value',     'b'=>'with space value',     'c'=>'single quote',     'd'=>'without_quote',    'e'=>'hello=world',    'f'=>'another weird format'    );*/$r=conv_attr($attr);print_r($r);/* currentResult:1*1*1*1*1*1*UnhandledKey[weird format"] Value[]Array(    [a] => a.value    [b] => with space value    [c] => single quote    [d] => ithout_quote    [e] => hello=world    [f] => "another)*/?>