• Home
  • About
  • Who Am I?
  •  

    PHP :: Serialize and Unserialize Alternative

    For quite sometime now I have been really annoyed by the unreliability of serialize and unserialize , when you would have a large string or some some special characters in there that wouldnt turn back into variables. So finally after not finding a decent alternative by someone else ( maybe I am only one suffering from the problem????? ) , I decided to write my own two functions.

    These two are working great for me , hope they serve some purpose to someone else. I am sure there is room for improvement here, please send in any suggestions or ideas to incorporate.

    Code (php)
    1.  
    2.         /// Usage
    3.         ////– first parameter is the var name , second is the var itself
    4.         $string = makeXML(’somevarname’ , $somevarname);
    5.  
    6.         ////— extractVars takes the above string as parameter ,
    7.         ////— and returns an arrray with variable name as key and variable itself
    8.         ///—  as value , i have used extract() to take all vars into code.
    9.         extract(extractVars($string));
    10.  
    11.  
    12.         function extractVars($xml , $name= , $type=){
    13.                 $reg="!<xmlvar name=\’(.*?)\’ type=’(.*?)’>(.*?)</xmlvar name=\’\\1\’>!s";
    14.                 preg_match_all($reg , $xml , $matches);
    15.  
    16.                 foreach($matches[1] as $index=>$key){
    17.                         $optname = $key;
    18.                         $opttype = $matches[2][$index];
    19.                         $optval = $matches[3][$index];
    20.  
    21.                         $optxpl = explode("::" , $optname);
    22.                         $optname = $optxpl[count($optxpl)-1];
    23.  
    24.                         if($opttype == ‘object’){
    25.                                 $bigarr[$optname] = (object)extractVars($optval , $optname , $opttype);
    26.                         }elseif($opttype==‘array’){
    27.                                 $bigarr[$optname] = extractVars($optval , $optname , $opttype);
    28.                         }else{
    29.                                 $bigarr[$optname] = $optval;
    30.                         }
    31.                 }
    32.  
    33.                 return $bigarr;
    34.         }
    35.  
    36.  
    37.  
    38.         function makeXML($name , $var="" , $parent = NULL ){
    39.                 if(!is_null($parent)) $parent = $parent."::";
    40.  
    41.                 if(is_object($var)){
    42.                         $xml .= "<xmlvar name=’$parent$name’ type=’object’>";
    43.                         foreach($var as $key=>$val){
    44.                                 $xml .= makeXML($key , $val , $parent.$name);
    45.                         }
    46.                         $xml .= "</xmlvar name=’$parent$name’>\r\n";
    47.                 }elseif(is_array($var)){
    48.                         $xml .= "<xmlvar name=’$parent$name’ type=’array’>";
    49.                         foreach($var as $key=>$val){
    50.                                 $xml .= $tab.makeXML($key , $val , $parent.$name);
    51.                         }
    52.                         $xml .= "</xmlvar name=’$parent$name’>\r\n";
    53.                 }elseif(is_bool($var)){
    54.                         $xml .= "<xmlvar name=’$parent$name’ type=’bool’>";
    55.                         $xml .= "$var";
    56.                         $xml .= "</xmlvar name=’$parent$name’>\r\n";
    57.                 }else{
    58.                         $xml .= "<xmlvar name=’$parent$name’ type=’string’>";
    59.                         $xml .= "$var";
    60.                         $xml .= "</xmlvar name=’$parent$name’>\r\n";
    61.                 }
    62.                 return $xml;
    63.         }
    64.  

    One Response to “PHP :: Serialize and Unserialize Alternative”

    1. simply24 » Blog Archive » PHP :: Serialize and Unserialize Alternative Says:

      […] here for […]

    Leave a Reply