Bir dize tefrika olup olmadığını görmek için kontrol edin?

6 Cevap php

Bir dize serialize sonucu / çıkış () fonksiyonu olup olmadığını belirlemek için en iyi yolu nedir?

6 Cevap

I unserialize bunu deneyin derim ;-)

Manuel alıntı:

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

Yani, dönüş değeri false veya (with === veya !== ise herhangi bir sorun var emin olmak için, denetlemek zorunda {[(3) ]} veya null veya false, I) derdim eşittir şey.

Sadece haber dikkat: @ operator kullanmak / ihtiyaç isteyebilirsiniz.

Örneğin:

$str = 'hjkl';
$data = @unserialize($str);
if ($data !== false) {
    echo "ok";
} else {
    echo "not ok";
}

Alacak:

not ok


EDIT : Oh, and like @Peter said (thanks to him!), you might run into trouble if you are trying to unserialize the representation of a boolean false :-(

Yani, tefrika dize eşit olmadığını kontrol "b:0;" çok yararlı olabilir; böyle bir şey hile yapmak gerekir, herhalde:

$data = @unserialize($str);
if ($str === 'b:0;' || $data !== false) {
    echo "ok";
} else {
    echo "not ok";
}

Sık sık yanlış bir tefrika değer yoksa, ama muhtemelen bu yararlı - nesneleştirmek denemeden önce bu özel davayı test bir optimizasyon olacaktır.

Ben aslında WordPress bulunuyor, bu kod yazmadım. Ben ilgilenen herkes için dahil düşündüm, overkill olabilir ama işe yarıyor :)

<?php
function is_serialized( $data ) {
    // if it isn't a string, it isn't serialized
    if ( !is_string( $data ) )
        return false;
    $data = trim( $data );
    if ( 'N;' == $data )
        return true;
    if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
        return false;
    switch ( $badions[1] ) {
        case 'a' :
        case 'O' :
        case 's' :
            if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
                return true;
            break;
        case 'b' :
        case 'i' :
        case 'd' :
            if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
                return true;
            break;
    }
    return false;
}

Eğer bu başka bir yol yaklaşım olabilir eğer Pascal Martin mükemmel cevap rağmen, merak ettim, bu yüzden sadece bir zihinsel egzersiz olarak yaptım

<?php

ini_set( 'display_errors', 1 );
ini_set( 'track_errors', 1 );
error_reporting( E_ALL );

$valueToUnserialize = serialize( false );
//$valueToUnserialize = "a"; # uncomment this for another test

$unserialized = @unserialize( $valueToUnserialize );

if ( FALSE === $unserialized && isset( $php_errormsg ) && strpos( $php_errormsg, 'unserialize' ) !== FALSE )
{
  echo 'Value could not be unserialized<br>';
  echo $valueToUnserialize;
} else {
  echo 'Value was unserialized!<br>';
  var_dump( $unserialized );
}

Ve aslında çalışır. Sadece ihtar nedeniyle tescilli hata işleyicisi varsa büyük olasılıkla kıracak nasıl $php_errormsg works.

$data = @unserialize($str);
if($data !== false || $str === 'b:0;')
    echo 'ok';
else
    echo "not ok";

Doğru serialize(false) davası işler. :)

Pascal Martin yanıtını optimize

/**
 * Check if a string is serialized
 * @param string $string
 */
public static function is_serial($string) {
    return (@unserialize($string) !== false);
}

If the $string is a serialized false value, ie $string = 'b:0;' SoN9ne's function returns false, it's wrong

bu nedenle fonksiyonu olacaktır

/**
 * Check if a string is serialized
 * @param string $string
 */
public static function is_serial($string) {
    return (@unserialize($string) !== false || $string == 'b:0;');
}