0

Hi I'm using this preg_replace_callback

$data = "a:6:{s:4:"home";s:32:"Rue d'Alsace Lorraine, Toulouse";s:6:"school";s:30:"Avenue de Fronton, Saint-Alban";s:9:"last_name";s:5:"Devis";s:10:"first_name";s:4:"Test";s:5:"phone";s:10:"0611223344";s:12:"got-distance";s:1:"1";}";


$data = preg_replace_callback(
    '!s:(\d+):"(.*?)";!se',
    function($matches) { return "'s:'.strlen('$2').':\"$2\";'"; },
    $data
);

but i'm the function returns null because e modifier not supported, anyone could help with this, I think I need to change the regular expression but I'm not good at regular expressions.

The goal of this is to pass $data to unserialize function.

jsf
  • 68
  • 3
  • 13
  • Please state what you're exactly trying to archive with your code – Tewdyn Jun 09 '18 at 15:20
  • 1
    Isn't it just serialized value that can be `unserialize`d? – u_mulder Jun 09 '18 at 15:27
  • I edited the code, it's now complete , if you can use this example and preg_replace_callback not returning null, that's why I'm looking for – jsf Jun 09 '18 at 15:27
  • Yes the goal of this is to unserialize it – jsf Jun 09 '18 at 15:28
  • 1
    In a callback you have `$matches` array, `print_r()` it, see what you have there and process. – u_mulder Jun 09 '18 at 15:30
  • For what strange reason do you have serialized data with wrong length? – Casimir et Hippolyte Jun 09 '18 at 15:36
  • I did but print_r not printing anything – jsf Jun 09 '18 at 15:36
  • Please just take the example, it's complete, the preg_replace_callback shouldn't return null – jsf Jun 09 '18 at 15:40
  • i think with preg_replace_callback you do not need the e modifier and can easily delete it – Norman M Jun 09 '18 at 15:43
  • Yes but it returns null, if I removed e modifier – jsf Jun 09 '18 at 15:48
  • I need a workaround for this to work – jsf Jun 09 '18 at 15:48
  • It is asked every now and then, very frequently. There are a lot of answers. Really, just search a bit, and you will find out you need to use a variable instead of placeholders. `preg_replace_callback('!s:(\d+):"(.*?)";!s', function($matches) { return 's:'.strlen($matches[2]).':"'. $matches[2] . '";'; }, $data);` (adjust the replacement since it is not clear what exactly it should be). – Wiktor Stribiżew Jun 09 '18 at 17:29

0 Answers0