regex - php converting preg_replace to preg_replace_callback -
i'm working on old code, , ran across - fails:
preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $sobject); it tells me preg_replace e modifier deprecated, , use preg_replace_callback instead.
from understand, supposed replace 's:'.strlen('$2').':\"$2\";' part callback function replacement on match.
what don't quite get, regexp doing i'll replacing. it's part of bit take php serialized data stuffed in database field (dumb, know ...) broken length fields , fix them reinsertion.
so can explain bit doing, or should replace with?
use
preg_replace_callback('!s:(\d+):"(.*?)";!', function($m) { return 's:' . strlen($m[2]) . ':"' . $m[2] . '";'; }, $sobject); the !e modifier must removed. $2 backreferences must replaced $m[2] $m match object containing match value , submatches , passed anonymous function inside preg_replace_callback.
here demo digits after s: replaced $m[2] length:
$sobject = 's:100:"word";'; $res = preg_replace_callback('!s:(\d+):"(.*?)";!', function($m) { return 's:' . strlen($m[2]) . ':"' . $m[2] . '";'; }, $sobject); echo $res; // => s:4:"word";
Comments
Post a Comment