1

I have data stored in database tables as serialized.

One of my functions needs to return true if the value is serialized.

foreach($data as $r):
if($r->options is serialized) {
  $unserialized_value = unserialize($r->options);
}
endforeach;
Brad
  • 12,054
  • 44
  • 118
  • 187
  • 2
    It is not possible to detect whether it is a serialized data or the string that looks exactly like the serialized data. Also, if you have such issue - then it means that you spent not enough time while created the database architecture. – zerkms Mar 02 '11 at 02:39

2 Answers2

2

i think you should specify the column that you know that's serialized and apply this code

 function c($r=$result_array){

    $full_unserialize_array=array();
    foreach($r as $s){

     $p= @unserialize($s);
     if($p == false){
       $full_unserialize_array[]=$s;

     }else{
       $full_unserialize_array[]=$p;
     }//end else
}//end foreach
 return $full_unserialize_array;

 }//end function c
  print_r($full_unserialize_array);

i'm sorry i'm new here ^_^

`

owis sabry
  • 154
  • 9
1

there's a couple of functions out there:

is_serialized http://www.cs278.org/blog/2009/10/23/php-function-is_serialized/

couple more suggestions here: Check to see if a string is serialized?

Community
  • 1
  • 1
  • 1
    Yes, let's guess instead of creaing **correct** database structure! – zerkms Mar 02 '11 at 03:00
  • I make no judgement on the merits of the approach, I simple answered the question. –  Mar 02 '11 at 03:03
  • @Dagon: as i said in comments - you cannot say 100% correctly whether the string is a serialized data or just a regular string that contains data that looks like it is serialized. All you can do is just guess. – zerkms Mar 02 '11 at 03:11
  • 1
    are you going to keep digging? –  Mar 02 '11 at 03:13
  • @Dagon: `s:5:"hello";` and `s:5:"hello";`. Which one is a serialized string and which is a just a string that contains 3 literals separated by `:`? – zerkms Mar 02 '11 at 03:15
  • you can create as many *fake theoretical scenarios* as you wish, it changes nothing. –  Mar 02 '11 at 03:20
  • @Dagon: sure, no scenario will change the fact that OP's database structure is terrible and cannot store trustworthy data. – zerkms Mar 02 '11 at 03:22