0

Due to an update to PHP7, I need to replace two rows in a plugin with preg_replace to preg_replace_callback(). I feel it could be simple but I have no idea how to solve it.

The original was:

for ($i = 0; $i < count($links); $i++)
{   
    $link = $links[$i];
    if ($link['hasNum'] >= $link['maxNum']) continue;
    $replace = '<a' . ($linxCSS != '' ? ' class="' . $linxCSS . '"' : '') . ' href="' . $link['href'] . '">$2</a>';
    if ($linxCSS != '') $replace = '<span class="' . $linxCSS . '">' . $replace . '</span>';
    $replace = '$1' . $replace . '$3';
    $search = "~([\s\.\,\;\!\?\:\>\(\)\'\"\*\/«])(" . $link['words'] . ")([\*\/\'\"\(\)\<\s\.\,\;\!\?\:»])~siu";
    $body = preg_replace("~(<a)(.*?)(?=<\/a>)(<\/a>)~sie", '"<:ZyX>".plgSystemSeolinks::maskContent("$1$2$3")."<:ZyX/>"', $body);
    $body = preg_replace($search, $replace, $body, $link['maxNum']);
    if ($body == '') return false;
    $link['hasNum']+= substr_count($body, '<a ');
    $links[$i] = $link;   
}
Ivar
  • 6,138
  • 12
  • 49
  • 61
  • Howdy. Since you don't know php and it was a one-off thing to me, I actually did the conversion for you in my answer. When it was marked as a duplicate, I deleted it and you can't see it unfortunately. @WiktorStribiżew and Toto apparently want you to learn php from scratch to do this simple task. If you want to see it, let me know and I'll post it on a paste bin for you. –  Oct 19 '17 at 21:21
  • Hello sln, thanks for your kindness and understanding! Without any basics of programing I see very little behind php codes....I would really appreciate your conversion. Thanks Petr – user2635661 Oct 24 '17 at 19:55
  • I tried it and works great !!! You are the master, it is very useful to see this specific example. Unfortunately I have not enough reputation yet to vote here .... Thanks a lot for help! – user2635661 Oct 25 '17 at 12:33

0 Answers0