Php7.1 unserialize non-encoding string to uft8

Before Php 7.1 latin5 (ISO-8859-*) chars inside serialize string to utf8 page/database encoding system unserialize problem.

Error:

Notice: unserialize(): Error at offset x of y bytes in ..../..php on line 1.

Solution:

$string = preg_replace(
'!s:(\d+):"(.*?)";!se',
"'s:'.strlen('$2').':\"$2\";'",
$string
);

But, php 7.1 version preg_replace was changed. preg_replace_callback using for unserialize non-encoding chars to utf8 string.

This function is sample that, ISO-8859–9 encoding page and latin5 encoding database system create serialize.

$array = [
[
'adi' => 'HAMİDE YÜCE',
'cinsiyet' => 'KADIN',
'yas' => '32',
],
[
'adi' => 'AHMET EMİN YÜCE',
'cinsiyet' => 'ERKEK',
'yas' => '32',
],
[
'adi' => 'ENES KAAN YÜCE',
'cinsiyet' => 'ERKEK',
'yas' => '3',
]

];
   $arraySerialize = serialize($array);
/*! output */
/*
a:3:{i:0;a:3:{s:3:"adi";s:11:"HAMİDE YÜCE";s:8:"cinsiyet";s:5:"KADIN";s:3:"yas";s:2:"32";}i:1;a:3:{s:3:"adi";s:15:"AHMET EMİN YÜCE";s:8:"cinsiyet";s:5:"ERKEK";s:3:"yas";s:2:"32";}i:2;a:3:{s:3:"adi";s:14:"ENES KAAN YÜCE";s:8:"cinsiyet";s:5:"ERKEK";s:3:"yas";s:1:"3";}}
*/

This serialize string at Utf8 page and latin5 database encoding unserialize to fail, solve this problem use preg_replace_callback function.

/*! Php7.1 fail this
$string = preg_replace(
'!s:(\d+):"(.*?)";!se',
"'s:'.strlen('$2').':\"$2\";'",
$string
);
*/
/*! Php 7.1 solution*/
$arraySerialize = 'a:3:{i:0;a:3:{s:3:"adi";s:11:"HAMİDE YÜCE";s:8:"cinsiyet";s:5:"KADIN";s:3:"yas";s:2:"32";}i:1;a:3:{s:3:"adi";s:15:"AHMET EMİN YÜCE";s:8:"cinsiyet";s:5:"ERKEK";s:3:"yas";s:2:"32";}i:2;a:3:{s:3:"adi";s:14:"ENES KAAN YÜCE";s:8:"cinsiyet";s:5:"ERKEK";s:3:"yas";s:1:"3";}}';
$arraySerializeConvert = 
preg_replace_callback(
"%s:(\d+):\"(.*?)\"\;%",
function($matches){
return 's:'.strlen($matches[2]).':"'.$matches[2].'";';
}, $arraySerialize);

/*! Output
a:3:{i:0;a:3:{s:3:"adi";s:13:"HAMİDE YÜCE";s:8:"cinsiyet";s:5:"KADIN";s:3:"yas";s:2:"32";}i:1;a:3:{s:3:"adi";s:17:"AHMET EMİN YÜCE";s:8:"cinsiyet";s:5:"ERKEK";s:3:"yas";s:2:"32";}i:2;a:3:{s:3:"adi";s:15:"ENES KAAN YÜCE";s:8:"cinsiyet";s:5:"ERKEK";s:3:"yas";s:1:"3";}}
*/
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.