0

I'm looping through a database table which holds some data related to external API responses.

Someone was saving the responses as text, like so:

object(stdClass)#5 (1) {
  ["CreateProspectResult"]=>
  object(stdClass)#6 (3) {
    ["State"]=>
    string(7) "Success"
    ["ErrorMessage"]=>
    string(0) ""
    ["ReturnValue"]=>
    object(stdClass)#7 (1) {
      ["ProspectID"]=>
      int(304)
    }
  }
} 

I'm guessing the original developer was doing a var_dump() with the <pre> tags, and saved the response literally.

When I loop through the Table results, I'm want to convert the string back into an Object, so I can parse it. Which I'm failing at, badly.

Here's what I've attempted:

<?php

$string = '
object(stdClass)#5 (1) {
  ["CreateProspectResult"]=>
  object(stdClass)#6 (3) {
    ["State"]=>
    string(7) "Success"
    ["ErrorMessage"]=>
    string(0) ""
    ["ReturnValue"]=>
    object(stdClass)#7 (1) {
      ["ProspectID"]=>
      int(304)
    }
  }
}
';

$xmlObj = simplexml_load_string($string);
echo $xmlObj->CreateProspectResult->State;
?>

PHP isn't enjoying my attempts, and giving me non-object-related errors back.

Is it possible to take such a string and convert it back into an Object?

Prix
  • 19,417
  • 15
  • 73
  • 132
coffeemonitor
  • 12,780
  • 34
  • 99
  • 149
  • Update your question with an example of what the API returns or do you not have access to the API directly. Aside from that I don't think there is any ready to go converter from string to stdClass. – Prix Mar 28 '14 at 01:15
  • "Someone was saving the responses as text, like so:" --- looooooooool – zerkms Mar 28 '14 at 01:16
  • I don't have access to the API directly, unfortunately. I'm told the response was what is posted above. – coffeemonitor Mar 28 '14 at 01:18

1 Answers1

0

I'm not 100% sure if this works for object (seems to be), but this seems to cover your question.

function unvar_dump($str) {
    if (strpos($str, "\n") === false) {
        //Add new lines:
        $regex = array(
            '#(\\[.*?\\]=>)#',
            '#(string\\(|int\\(|float\\(|array\\(|NULL|object\\(|})#',
        );
        $str = preg_replace($regex, "\n\\1", $str);
        $str = trim($str);
    }
    $regex = array(
        '#^\\040*NULL\\040*$#m',
        '#^\\s*array\\((.*?)\\)\\s*{\\s*$#m',
        '#^\\s*string\\((.*?)\\)\\s*(.*?)$#m',
        '#^\\s*int\\((.*?)\\)\\s*$#m',
        '#^\\s*float\\((.*?)\\)\\s*$#m',
        '#^\\s*\[(\\d+)\\]\\s*=>\\s*$#m',
        '#\\s*?\\r?\\n\\s*#m',
    );
    $replace = array(
        'N',
        'a:\\1:{',
        's:\\1:\\2',
        'i:\\1',
        'd:\\1',
        'i:\\1',
        ';'
    );
    $serialized = preg_replace($regex, $replace, $str);
    $func = create_function(
        '$match', 
        'return "s:".strlen($match[1]).":\\"".$match[1]."\\"";'
    );
    $serialized = preg_replace_callback(
        '#\\s*\\["(.*?)"\\]\\s*=>#', 
        $func,
        $serialized
    );
    $func = create_function(
        '$match', 
        'return "O:".strlen($match[1]).":\\"".$match[1]."\\":".$match[2].":{";'
    );
    $serialized = preg_replace_callback(
        '#object\\((.*?)\\).*?\\((\\d+)\\)\\s*{\\s*;#', 
        $func, 
        $serialized
    );
    $serialized = preg_replace(
        array('#};#', '#{;#'), 
        array('}', '{'), 
        $serialized
    );

    return unserialize($serialized);
}

Source: Convert var_dump of array back to array variable

Community
  • 1
  • 1
Jordi Kroon
  • 2,607
  • 3
  • 31
  • 55