|
King of Portal
Join Date: Sep 2005
Posts: 431
Rep Power: 4 
|
Messing with Serialization
For a more detailed reason on why I'm doing this read here.
The idea behind this is that when PHP serializes an array it generates a string that contains all the information to recreate the array. However, I would like to be able to separate the array keys (keys that are exclusively strings) from the serialized string itself. So that I would have the keys in one string and the values of the array in another. I would also like to be able to recombine the two strings into the original serialized string. So here's the code I wrote up to do this: class gbSerializer {
var $serialString = '';
var $keys = '';
var $values = '';
function gbSerializer($keys, $values = null){
if(!isset($values)){
$this->serialString = $keys;
$count = 0;
do{
$lengthMarker = 0;
switch(substr($keys, 0, 1)){
case 'a':
$lengthMarker = strpos($keys, '{') + 1;
break;
case 's':
$lengthMarker = intval(substr($keys, 2, strpos($keys, ':', 2)));
$lengthMarker += strpos($keys, '"') + 3;
break;
case 'i':
case 'N':
case 'b':
$lengthMarker = strpos($keys, ';') + 1;
break;
case '}':
$lengthMarker++;
break;
}
$temp = substr($keys, 0, $lengthMarker);
$keys = substr($keys, $lengthMarker);
$iniChar = substr($temp, 0, 1);
switch($iniChar){
case 'a':
$count = 0;
$this->values .= $temp;
$this->keys .= '{';
break;
case '}':
$count = 0;
$this->values .= $temp;
$this->keys .= '}';
break;
default:
switch($count % 2){
case 0:
if(!strcmp($iniChar, 'i')){
$this->values .= $temp;
}else{
$this->keys .= $temp;
}
break;
case 1:
$this->values .= $temp;
break;
}
$count++;
break;
}
}while(strlen($keys) > 0);
}else{
$this->keys = $keys;
$this->values = $values;
do{
$lengthMarker = 0;
switch(substr($keys, 0, 1)){
case '{':
$keys = substr($keys, 1);
$lengthMarker = strpos($values, '{') + 1;
$this->serialString .= substr($values, 0, $lengthMarker);
$values = substr($values, $lengthMarker);
break;
case '}':
$lengthMarker++;
$this->serialString .= substr($values, 0, $lengthMarker);
$keys = substr($values, $lengthMarker);
$values = substr($values, $lengthMarker);
break;
default:
// will produce an error that can be ignored
$lengthMarker = intval(substr($keys, 2, @strpos($keys, ':', 2)));
$lengthMarker += strpos($keys, '"') + 3;
$temp = substr($keys, 0, $lengthMarker);
$this->serialString .= $temp;
$lengthMarker++;
$arrFlag = false;
switch(substr($keys, $lengthMarker, 1)){
case ':':
$lengthMarker--;
break;
case '}':
$lengthMarker++;
$arrFlag = true;
break;
}
$keys = substr($keys, $lengthMarker);
do{
$lengthMarker = 0;
switch(substr($values, 0, 1)){
case 'a':
$lengthMarker = strpos($values, '{') + 1;
break;
case 's':
$lengthMarker = intval(substr($values, 2, strpos($values, ':', 2)));
$lengthMarker += strpos($values, '"') + 3;
break;
case 'i':
case 'N':
case 'b':
$lengthMarker = strpos($values, ';') + 1;
break;
case '}':
$lengthMarker++;
$arrFlag = false;
break;
}
$this->serialString .= substr($values, 0, $lengthMarker);
$values = substr($values, $lengthMarker);
}while($arrFlag);
if(strlen($values) == 0 && strlen($keys) != 0){
$this->serialString = substr($this->serialString, 0, -1 * strlen($temp) - 1) . '}';
}
break;
}
}while(strlen($values) > 0);
}
}
function getKeys(){
return $this->keys;
}
function getValues(){
return $this->values;
}
function getSerial(){
return $this->serialString;
}
} Pretty lengthy as you can tell. Now I'm pretty confident about the constructor when it is given a serialized string and it removes the keys (it also inserts brackets into the keys so as to indicate the nested structure of the array). However, I'm not so confident about the constructor when it is given keys and values to try and output a serialized string. It works with what I've tried, but there are instances where an error is generated which I just put in an @ to ignore. I was hoping someone with some free time might be able to offer an alternative. Thanks for any help.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
|