Jump to content

[SOLVED] serialization help :(


sasori

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/169475-solved-serialization-help/
Share on other sites

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

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

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"}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.