Reputation: 73
I have the following as a string:
stdClass Object
(
[createResult] => stdClass Object
(
[ReturnCode] => 1
)
)
How can I take the above string and create a new stdClass Object? I'd like to get the value like this:
$rc = $obj->createResults->ReturnCode;
Upvotes: 0
Views: 287
Reputation: 9202
You can also do it like this:
<?php
$s = "stdClass Object
(
[createResult] => stdClass Object
(
[ReturnCode] => 1
)
)";
$s = preg_match("/\s\[ReturnCode\] => \S+\s/", $s, $m);
echo preg_replace("/\s\[ReturnCode\] => (\S+)\s/", "$1", $m[0]);
Upvotes: 0
Reputation: 132018
If you can't change it (for whatever reason) and have to use the output of print_r, then you could try this recipe and see if it works for you. http://www.php.net/manual/en/function.print-r.php#93529
However, if at all possible, you should be storing data in a more portable format. You could use php's serialize => unserialize, or json_encode => json_decode, for starters.
Upvotes: 1
Reputation: 2610
If you can change the way it outputs into something like var_export
, you can afterwards use that string with eval
to get it back.
http://www.php.net/manual/en/function.var-export.php
Upvotes: 1