-1

I'm in the process of migrating from PHP5.x to PHP 7.2 and part of my link converter function is generating a warning:

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead

Here's the function that's triggering the warning:

function linkify($str) {
    $ret = ' ' . $str;
    $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a class=\"offsite\" href=\"\\2\" target=\"_blank\" rel=\"nofollow\" >\\2</a>'", $ret);
    $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a class=\"offsite\" href=\"http://\\2\" target=\"_blank\" rel=\"nofollow\" >\\2</a>'", $ret);
    $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a class=\"offsite\" href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
    $ret = substr($ret, 1);
    return($ret);
}

I'm getting 2 identical warnings, one for the first preg_replace, another for the second. Somehow the third isn't tiggering a warning.

Unless I'm blind, it's not using the /e modifier, so I can't figure out what's causing the problem.

byron
  • 984
  • 3
  • 14
  • 26

1 Answers1

2

Here the e modifier is useless since preg_replace doesn't refer to any function in the replacement parameter. You can write:

$ret = preg_replace('#(?<!\S)\w+://\w+[^"\s<]*#', '<a class="offsite" href="$0" target="_blank" rel="nofollow">$0</a>', $ret);
$ret = preg_replace('#(?<!\S)(?:www|ftp)\.[^"\s<]*#i', '<a class="offsite" href="http://$0" target="_blank" rel="nofollow">$0</a>', $ret);
$ret = preg_replace('#(?<!\S)[\w&.-]+@(?:[\w-]+\.)+\w+#', '<a class="offsite" href="mailto:$0">$0</a>', $ret);
revo
  • 47,783
  • 14
  • 74
  • 117
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125