Re: [PHP] Re: Storing data structires in DB
On Fri, April 1, 2005 10:49 am, GamblerZG said: > And please do not tell me that I need to write it in C++. The thing should > be portable. Write it in C, not C++, as a PHP extension. http://talks.php.net -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Storing data structires in DB
Ok, I wrote something that fits my needs. But, as I said, it is slow. Too slow. Is anyone except me interested in human-editable serialize? Can anyone help me with optimization? And please do not tell me that I need to write it in C++. The thing should be portable. == define('CMS_ARR_BEGIN', 1); define('CMS_ARR_END', 2); define('CMS_COMA', 3); define('CMS_ARROW', 4); define('CMS_SCALAR', 5); function encode($var) { if (is_array($var)) { $code = '('; foreach ($var as $key => $value) { $code .= encode($key).'='.encode($value).','; } $code = chop($code, ','); //remove unnecessary coma $code .= ')'; return $code; } else { if (is_string($var)) { if (strpos($var, "'") !== FALSE) { $var = str_replace("'", "''", $var); } return "'".$var."'"; } elseif (is_numeric($var)) { return $var; } elseif (is_bool($var)) { return ($var ? 'T' : 'F'); } else { return 'N'; } } } function decode($str){ $stack = array(); $scalars = array(); $strLen = strlen($str); while ($ptr < $strLen) { $ptrChar = $str{$ptr}; if (preg_match('/\s/', $ptrChar )) { //do nothing } else { if ($ptrChar == '(') { $stack[] = CMS_ARR_BEGIN; } elseif ($ptrChar == ')') { $arrBegins = array_pop(array_keys($stack, CMS_ARR_BEGIN)); if ($arrBegins === FALSE) { user_error("Unexpected ')'", E_USER_WARNING); return; } $arrTokens = array_splice($stack, $arrBegins + 1); //get array content array_pop($stack); //remove beginning token if (empty($arrTokens)) { //empty array $scalars[] = array(); $stack[] = CMS_SCALAR; continue; } $arrScalars = array(); foreach ($arrTokens as $token) { if ($token == CMS_SCALAR) { $arrScalars[] = array_pop($scalars); /*arrScalars are now reversed, so first token is the last scalar*/ } } if ($arrTokens[sizeof($arrTokens) - 1] != CMS_COMMA) {//for symmetry array_push($arrTokens, CMS_COMMA); } $arrBuffer = array(); reset($arrTokens); while (list(, $token) = each($arrTokens)) { if ($token == CMS_SCALAR) { list(,$nextTok) = each($arrTokens); if ($nextTok == CMS_COMMA) { $arrBuffer[] = array_pop($arrScalars); continue; } elseif ($nextTok == CMS_ARROW) { list(, $valTok) = each($arrTokens); if ($valTok != CMS_SCALAR) { var_dump($arrTokens); var_dump($arrScalars); user_error("Invalid token encountered during array compression: $valKey => $valTok", E_USER_WARNING); return; } $arrBuffer[array_pop($arrScalars)] = array_pop($arrScalars); list($valKey, $valTok) = each($arrTokens); if ($valTok != CMS_COMMA) { var_dump($arrTokens); var_dump($arrScalars); user_error("Invalid token encountered during array compression: $valKey => $valTok", E_USER_WARNING); return; } } else { echo "Array compression dump:\n"; var_dump($arrTokens); var_dump($arrScalars); user_error("Invalid token encountered during array compression:$ntk => $nextTok", E_USER_WARNING); return; } } else { echo "Array compression dump:\n"; var_dump($arrTokens); var_dump($arrScalars); user_error("Invalid token encountered during array compression:$key => $token", E_USER_WARNING); return; } } $scalars[] = $arrBuffer; //now multiple scalars replaced with array itself $stack[] = CMS_SCALAR; } elseif (preg_match('/["\']/', $ptrChar)) { $qEnd = strpos($str, $ptrChar, $ptr + 1); while ($str{$qEnd + 1} == $str{$qEnd}) { //deal with escapes $q
Re: [PHP] Re: Storing data structires in DB
You can have the functions list in an array and check if any of that is in the code you are going to pass to eval(). Devta. GamblerZG escribió: Ok, let me ask in a different way. Is there any way to make eval to parse only data structures, without executing any functions it may encounter? __ Renovamos el Correo Yahoo!: ¡250 MB GRATIS! Nuevos servicios, más seguridad http://correo.yahoo.es -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Storing data structires in DB
Ok, let me ask in a different way. Is there any way to make eval to parse only data structures, without executing any functions it may encounter? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Storing data structires in DB
"Joshua Beall" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > You should look into the WDDX functions - http://php.net/wddx/ - they give > you an XML document that you can edit by hand much more easily than the > bytestream you get from serialize. However it is not as compact as > serialize, and not only that it suffers from what I consider a showstopped > bug. er, make that "showstopper..." -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Storing data structires in DB
"GamblerZG" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Output of serialize() is barely readable and definetely is not suited for > manual editing. > > It is quite simple to create var_export() clone that does not add junk to > it's output. But then I would need to exec() the string to get a data > structure back, which is bad security practice. > > Is there any good way to store/retrieve data structures (multidimetional > arrays) to/from database? You should look into the WDDX functions - http://php.net/wddx/ - they give you an XML document that you can edit by hand much more easily than the bytestream you get from serialize. However it is not as compact as serialize, and not only that it suffers from what I consider a showstopped bug. This bug in the WDDX serialization causes you to run into trouble if you have a numerically indexed array that does not start at 0. For instance: //$data[0] = 'uncomment me and things will work'; $data[1] = "foo"; $data[2] = "bar"; $serialized = wddx_serialize_value($data); $result = wddx_deserialize($serialized); echo gettype($result[1]); The output is "NULL" - it is now impossible directly access anything in the $result array. It is still there - you can see this if you var_dump($result) or iterate through it with foreach($result as $entry). HTH, -Josh -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php