* @Date: December 27th, 2007 * @Description: * This function lets you take a string in the format of an array when defined in php such as * array ( 'a' => array ( 'b' => 'c', 'd' => 'e', ), 'f' => 'g', 'h' => array ( 'i' => 'j', 'k' => 'l', ), ) * when passed as a string is returned as array. This is the equiv of doing $array = eval('return '.$myarray.';'); * however, this does not use eval so it does not pose any security risk! This is a much safer * way of converting a string representation of an array to an actual array. This is not meant to * provide an alternate method to serialize/unserialize. This is meant for situations where you * or user would need to input data from a string input to pass arrayed information. * * Example Below Function ***************************************/ function var_import($string) { //Trim it up $string=trim($string); //Did it start with array? the initial array declaration isnt needed if(substr($string,0,5) == 'array') { //clean off array( from start and ); from end $string = substr(trim(substr(rtrim($string,"; "),5,strlen($string)-6)),1); } $bS = false; //bool for open single quote $bD = false; //bool for open double quote $last = ''; //last char read before current that wasnt a space $newstring = ''; //the new rebuilt string for($i = 0;$i < strlen($string); $i++) { //Get the 1 character $c = substr($string,$i,1); //If its single or double quote and the boolean is false, set to true. Flag if the quotes are 'open' if($c == '"') $bD = !$bD; if($c == "'") $bS = !$bS; //were using , as a seperator, we need to make sure there is a , before every ) that //isnt enclosed in brackets. if(!$bD && !$bS && $c == ')' && $last != ',') $newstring .= ','; //append to the new string $newstring .= $c; //we need to seperate ( from the key/value pairs after it, so add 'array' which is a seperator for the split if(!$bD && !$bS && $c == '(') $newstring .= 'array'; //set the last char that wasnt a space if($c != ' ') $last = $c; } //split the string. This splits elements into either a key=>value pair, // a ( to signal nesting a level deep, or a ) to signal going back a level of nesting. // or finally a blank entry signalling the current key is for the next nested array $chunks = split('(array|,)',$newstring); //Our initial array $array = array(); //This is a pointer to the current nested level of the array. //We will constantly change this as we go deeper into nesting. $p =& $array; //the current depth level of the nest, used with $pL //$pL<$i> will hold a pointer back to that depth level of the array. //This lets us return back down a level of the array to continue building it $i = 0; //bKey boolean, signalling if the next nested element has a key name $bK = false; //this is the name of the key the next nested array should be set to. $bkey = ''; //Run through our chunks. foreach($chunks as $chunk) { //Clean any junk $chunk = trim($chunk); //Make sure we actually got some data, some entries can result in empty data. if(strlen($chunk) > 0) { //Is it a closing parenthese? Decrement the pointer. if($chunk == ')') { //If depth $i was currently on 3, we return $p to the depth level of 2 ($pL2). $p =& ${'pL'.--$i}; } //Is it an opening parenthese? Increment the pointer else if($chunk == '(') { //If depth was 0, store $pL0 to the CURRENT depth. ${'pL'.$i++} =& $p; //if a keyname was set, use it and assign the pointer to the next level in the array if($bK == true) { $p =& $p[$bkey]; $bK = false; }else{ //No key name given, just let php assign the keyname and point to that. $p =& $p[]; } } else //We have a possible pair to enter { //has a seperator for key=>value if(strpos($chunk,'=>')) { list($key,$value) = explode('=>',$chunk,2); }else{ //no key name given, just a value. $key = ''; $value = $chunk; } //trim our data $key = trim($key); $value = trim($value); //some tricky foreach to prevent having to do this code twice for $key and $value //if the value starts with ' then remove ' from beginning and end of value. //do the same for " foreach(array('key' => $key, 'value' => $value) as $sKey => $sValue) { //is it a quote? if(substr($sValue,0,1) == '"' || substr($sValue,0,1) == "'") { //Remove the first and last character. If $sKey is 'key', then $key is set. $$sKey = substr($sValue,1,strlen($sValue)-2); } } //Check if we had a value if(strlen($value) == 0) { //We had a keyname... but no value.. this means the key name is the key name for the next nested array, store it $bK = true; $bkey = $key; }else{ //we have a value, but did we have a key name? if(strlen($key)>0) { //Add with the key $p[$key] = $value; }else{ //Let PHP generate the key name $p[] = $value; } } } } } //Return our finished array return $array; } /*********** * EXAMPLE CODE ***********/ $array = array ( 'a' => array ( 'b' => 'c', 'd' => 'e', ), 'f' => 'g', 'h' => array ( 'i' => 'j', 'k' => 'l', ), ); echo '
';
$export = var_export($array,true);
echo "Contents from var_export of a properly formatted array:\n";
echo $export;
echo "\nprint_r of ACTUAL array:\n";
print_r($array);
echo "\nprint_r of array built from var_import:\n";
print_r(var_import($export));
echo "\nThe 2 print_r's should be identical.\nHeres a manually created print_r array:\n";
$stringarray="array('a' => 'b', 'c' => array('d' => 'e'));";
print_r(var_import($stringarray));