Need help with rewriting preg_replace to preg_replace_callback :

 $send_mess = preg_replace("/private \[(.*)\]/Ue","'<span class=user-to-private data=\"'.(('\\1' != '" . $user['login'] . "')?'\\1':'" . $send_login ."').'\">private [</span><span class=user-to-private name=\"\\1\" data=\"'.(('\\1' != '". $user['login'] . "')?'\\1':'" . $send_login ."').'\">\\1</span><span class=user-to-private data=\"'.(('\\1' != '". $user['login'] . "')?'\\1':'" . $send_login ."').'\">]</span>'", $send_mess); 

PS I read the manuals, but nothing happened. Thank you in advance.

  • I apologize, I did not understand a little and put in a four-hit ( - Alexander
  • I apologize in advance, I initially did not understand the interface - Alexander
  • the way it was originally so)) is the original $ send_mess line, it looks for "private" and is replaced with the nightmare that goes on. The trouble is that it stops working in php7.2, based on their manual, you need to replace the preg_replace with preg_replace_callback. accordingly, this is the problem. lack of understanding - Alexander
  • The line is taken from the working project under pkhp5.4. pkhp7.2 swears on the modifier / e and does not execute this code. because of this, it is necessary to use preg_replace_callback. but, the string itself is complicated and I am at a dead end - Alexander

2 answers 2

The line is taken from the working project under pkhp5.4. pkhp7.2 swears on the modifier / e and does not execute this code. because of this, it is necessary to use preg_replace_callback

On preg_replace_callback (), the code will look like this:

 $send_mess = preg_replace_callback( '~private \[(.*?)\]~', function ($m) use (&$user, &$send_login){ return '<span class=user-to-private data="' . ($m[1] != $user['login'] ? $m[1] : $send_login) . '">private [</span><span class=user-to-private name="' . $m[1] . '" data="' . ($m[1] != $user['login'] ? $m[1] : $send_login) . '">' . $m[1] . '</span><span class=user-to-private data="' . ($m[1] != $user['login'] ? $m[1] : $send_login) . '">]</span>'; }, $send_mess ); echo $send_mess; 

It returns the same string as your analog. Well, if you say that this is necessary, then so be it. Although of course read this code is not realistic.

  • thank you for help. the author of this creation is not me, I am faced with the need to adapt this "code" under pkhp7.2 - Alexander

Required data:

 $user['login'] = 'Юзер_Логин'; $send_login = 'Логин'; $send_mess = 'private [что-то]'; 

The rewritten construction itself:

 $send_mess = preg_replace_callback( '/private \[(.*)]/U', function ($call) use ($user, $send_login) { $logic = $call[1] != $user['login'] ? $call[1] : $send_login; return " <span class=user-to-private data=\"{$logic}\">private [</span> <span class=user-to-private name=\"{$call[1]}\" data=\"{$logic}\">{$call[1]} </span><span class=user-to-private data=\"{$logic}\">]</span> "; }, $send_mess );