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
Share on other sites

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..

Link to comment
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

Link to comment
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

 

sir, can you give me an example of this?,,you can use the code i posted

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.