sasori Posted August 9, 2009 Share Posted August 9, 2009 here's the first code class SampleObject{ public $var1; private $var2; protected $var3; static $var4; public function __construct(){ $this->var1 = "Value One"; $this->var2 = "Value Two"; $this->var3 = "Value Three"; SampleObject::$var4 = "Value Four"; } } $so = new SampleObject(); $serializedso = serialize($so); file_put_contents("wordtocount.txt",$serializedso); echo $serializedso; run it then browser response is O:12:"SampleObject":3:{s:4:"var1";s:9:"Value One";s:18:"�SampleObject�var2";s:9:"Value Two";s:7:"�*�var3";s:11:"Value Three";} now here's the 2nd code include_once("class.serialization.php"); $serialize = file_get_contents("wordtocount.txt"); $unserialized = unserialize($serialize); print_r($unserializedcontent); now system response is: O:12:"SampleObject":3:{s:4:"var1";s:9:"Value One";s:18:"�SampleObject�var2";s:9:"Value Two";s:7:"�*�var3";s:11:"Value Three";} Now my question is, "Is there a way to print the output into a nice way after serialization?", I would like to see the output similar to this ( [var1] => Value One [var2:private] => Value Two [var3:protected] => Value Three ) so how am i gonna do that? to show it on a nicer way? Quote Link to comment https://forums.phpfreaks.com/topic/169475-solved-serialization-help/ Share on other sites More sharing options...
wildteen88 Posted August 9, 2009 Share Posted August 9, 2009 wrap print_r in <pre></pre> tags echo '<pre>' . print_r($unserializedcontent, true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/169475-solved-serialization-help/#findComment-894168 Share on other sites More sharing options...
sasori Posted August 9, 2009 Author Share Posted August 9, 2009 wrap print_r in <pre></pre> tags echo '<pre>' . print_r($unserializedcontent, true) . '</pre>'; I already done that before i even posted this question here,, but the output is the same,,its the same weird combination of characters.. Quote Link to comment https://forums.phpfreaks.com/topic/169475-solved-serialization-help/#findComment-894403 Share on other sites More sharing options...
oni-kun Posted August 10, 2009 Share Posted August 10, 2009 Note: Object's private members have the class name prepended to the member name; protected members have a '*' prepended to the member name. These prepended values have null bytes on either side. If you don't need to put it into a database, I'd recommend you use json_encode() for it is much more what you'd use without needing to set it into a special array, json_decode would place the file contents into an array after. http://ca.php.net/json_encode Quote Link to comment https://forums.phpfreaks.com/topic/169475-solved-serialization-help/#findComment-894431 Share on other sites More sharing options...
sasori Posted August 10, 2009 Author Share Posted August 10, 2009 Note: Object's private members have the class name prepended to the member name; protected members have a '*' prepended to the member name. These prepended values have null bytes on either side. If you don't need to put it into a database, I'd recommend you use json_encode() for it is much more what you'd use without needing to set it into a special array, json_decode would place the file contents into an array after. http://ca.php.net/json_encode sir, can you give me an example of this?,,you can use the code i posted Quote Link to comment https://forums.phpfreaks.com/topic/169475-solved-serialization-help/#findComment-894433 Share on other sites More sharing options...
oni-kun Posted August 10, 2009 Share Posted August 10, 2009 Something like this you can do.. function objectArray( $object ) { if ( is_array( $object )) return $object ; if ( !is_object( $object )) return false ; $serial = serialize( $object ) ; $serial = preg_replace( '/O:\d+:".+?"/' ,'a' , $serial ) ; if( preg_match_all( '/s:\d+:"\\0.+?\\0(.+?)"/' , $serial, $ms, PREG_SET_ORDER )) { foreach( $ms as $m ) { $serial = str_replace( $m[0], 's:'. strlen( $m[1] ) . ':"'.$m[1] . '"', $serial ) ; } } return @unserialize( $serial ) ; } class SampleObject{ public $var1; private $var2; protected $var3; static $var4; public function __construct(){ $this->var1 = "Value One"; $this->var2 = "Value Two"; $this->var3 = "Value Three"; SampleObject::$var4 = "Value Four"; } } $so = new SampleObject(); file_put_contents("wordtocount.txt",$so); echo "<pre>"; print_r( objectArray( $so )) ; echo "<br/>"; print_r(json_encode(objectArray($so))); And those will output.. Array: Array ( [var1] => Value One [var2] => Value Two [var3] => Value Three ) JSON (Compact): {"var1":"Value One","var2":"Value Two","var3":"Value Three"} Quote Link to comment https://forums.phpfreaks.com/topic/169475-solved-serialization-help/#findComment-894444 Share on other sites More sharing options...
sasori Posted August 10, 2009 Author Share Posted August 10, 2009 wow,nice..thanks for this sir ^^, Quote Link to comment https://forums.phpfreaks.com/topic/169475-solved-serialization-help/#findComment-894446 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.