开发者

How can I test for a serialized variable? Without throwing E_NOTICE if string is not serialized

I am coding my own var_dump to meet certain requirements which probably only apply to me and have a problem.

The code checks for objects, arrays, etc and finally gets to a stage where it reckons it is left with a number, a string or a boolean.

Of course, a string can actually be a serialized variable, so I want to heck for that ...

if (is_string($variable))
{
   // check if it is serialzed; if so, unserialize & dump as an array, \
   // with a suitable header indicating that it was serialized
   try
   {
      $old_error_level= error_reporting(E_ALL ^ E_NOTICE);
      $unserialized_varaible = @unserialize($variable);
      $result .= my_dump($unserialized_varaible, ... <some params&开发者_如何学编程gt;); // recursive call
      $old_error_level= error_reporting($old_error_level);
   }

   catch(Exception $e)    // Treat it as a string
   {
       $old_error_level= error_reporting($old_error_level);
       $result .= GetHtmlForSimpleVariable($variable, ... <some params>);
   }
}

But, what I get when trying to dump a simple, non-serialized, string is

Problem type Notice "unserialize() 
  [<a href='function.unserialize'>function.unserialize</a>]:
  Error at offset 0 of 14 bytes" at line 362 in file my_dump.php<br><br>

Update: the point there is that I want to suppress that E_NOTICE when a string is not a serialized string. I had thought that the @on @unserizlize() would do that, but ...

If the string is serialized then everything is hunky dory. If not, then not.


When you try to unserialize it, it returns false if it is not serialized. It also returns a an E_NOTICE which is where that output is coming from.

From the manual:

In case the passed string is not unserializeable, FALSE is returned and E_NOTICE is issued.

Check if the return value of unserialize ===false

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜