-1

i use that function to convert URLs to active links

public function make_links_blank($text)
     {
        return  preg_replace(
   array(
   '/(?(?=<a[^>]*>.+<\/a>)
         (?:<a[^>]*>.+<\/a>)
         |
         ([^="\']?)((?:https?|ftp|bf2|):\/\/[^<> \n\r]+)
     )/iex',
   '/<a([^>]*)target="?[^"\']+"?/i',
   '/<a([^>]+)>/i',
   '/(^|\s)(www.[^<> \n\r]+)/iex',
   '/(([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-]+)
   (\\.[A-Za-z0-9-]+)*)/iex'
   ),
 array(
   "stripslashes((strlen('\\2')>0?'\\1<a href=\"\\2\">\\2</a>\\3':'\\0'))",
   '<a\\1',
   '<a\\1 target="_blank" rel="nofollow">',
   "stripslashes((strlen('\\2')>0?'\\1<a        href=\"http://\\2\">\\2</a>\\3':'\\0'))",
   "stripslashes((strlen('\\2')>0?'<a href=\"mailto:\\0\">\\0</a>':'\\0'))"
         ),
        $text
   );
}

but i have an error

preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

  • http://php.net/manual/en/function.preg-replace-callback.php its expected that your callback returns a string, not an array... – LJᛃ Mar 25 '15 at 22:53
  • You can't pass an array of patterns and replacement strings with `preg_replace_callback`. You need to iterate over them, and invoke it multiple times. Also the callback function needs to contain the actual code, not just return the previous substitution inline expressions. – mario Mar 25 '15 at 22:54
  • What's your question then? Have you researched the things said in the error message? – imz -- Ivan Zakharyaschev Mar 26 '15 at 00:00
  • yes ,and i tried to use preg_replace_callback instead of preg_replace but i get error every time – Mohamed Maree Mar 26 '15 at 14:25

1 Answers1

0

i used that code instead and it work well

    public function make_links_blank($text=''){ 
      $rexProtocol = '(https?://)?';
      $rexDomain   = '((?:[-a-zA-Z0-9]{1,63}\.)+[-a-zA-Z0-9]{2,63}|(?:[0-9]{1,3}\.){3}[0-9]{1,3})';
      $rexPort     = '(:[0-9]{1,5})?';
      $rexPath     = '(/[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]*?)?';
      $rexQuery    = '(\?[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
      $rexFragment = '(#[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';    
      return $text =  preg_replace_callback("&\\b$rexProtocol$rexDomain$rexPort$rexPath$rexQuery$rexFr agment(?=[?.!,;:\"]?(\s|$))&",
   function ($match)
  {
    $completeUrl = $match[1] ? $match[0] : "http://{$match[0]}";
    return '<a href="' . $completeUrl . '" rel="nofollow" target="_blank">'
    . $match[2] . $match[3] . $match[4] . '</a>';
 }
 , htmlspecialchars($text));
}