3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function wd_check_serialization( $string, &$errmsg ) { $str = 's'; $array = 'a'; $integer = 'i'; $any = '[^}]*?'; $count = '\d+'; $content = '"(?:\\\";|.)*?";'; $open_tag = '\{'; $close_tag = '\}'; $parameter = "($str|$array|$integer|$any):($count)" . "(?:[:]($open_tag|$content)|[;])"; $preg = "/$parameter|($close_tag)/"; if( !preg_match_all( $preg, $string, $matches ) ) { $errmsg = 'not a serialized string'; return false; } $open_arrays = 0; foreach( $matches[1] AS $key => $value ) { if( !empty( $value ) && ( $value != $array xor $value != $str xor $value != $integer ) ) { $errmsg = 'undefined datatype'; return false; } if( $value == $array ) { $open_arrays++; if( $matches[3][$key] != '{' ) { $errmsg = 'open tag expected'; return false; } } if( $value == '' ) { if( $matches[4][$key] != '}' ) { $errmsg = 'close tag expected'; return false; } $open_arrays--; } if( $value == $str ) { $aVar = ltrim( $matches[3][$key], '"' ); $aVar = rtrim( $aVar, '";' ); if( strlen( $aVar ) != $matches[2][$key] ) { $errmsg = 'stringlen for string not match'; return false; } } if( $value == $integer ) { if( !empty( $matches[3][$key] ) ) { $errmsg = 'unexpected data'; return false; } if( !is_integer( (int)$matches[2][$key] ) ) { $errmsg = 'integer expected'; return false; } } } if( $open_arrays != 0 ) { $errmsg = 'wrong setted arrays'; return false; } return true; } var_dump(wd_check_serialization('a:1:{i:0;a:10:{s:2:"id";s:3:"390";s:12:"variation_id";s:3:"393";s:4:"name";s:38:"Serviette Absolut mit Gastro-Logodruck";s:3:"qty";s:1:"1";s:9:"item_meta";a:3:{i:0;a:2:{s:9:"meta_name";s:51:"Geben Sie bitte hier die gew├╝nschte Zusatzfarbe an";s:10:"meta_value";s:12:"schwarzgold";}i:1;a:2:{s:9:"meta_name";s:6:"pa_vpe";s:10:"meta_value";s:6:"18-900";}i:2;a:2:{s:9:"meta_name";s:12:"pa_erstdruck";s:10:"meta_value";s:2:"ja";}}s:13:"line_subtotal";s:3:"360";s:17:"line_subtotal_tax";s:4:"68.4";s:10:"line_total";s:3:"360";s:8:"line_tax";s:4:"68.4";s:9:"tax_class";s:19:"gesetzliche-mwst-19";}}',$err));
based on DI4Y5
Output for 4.3.0 - 7.1.0
bool(false)