Search code examples
phpregexhyperlinkprotocols

ERROR with regexp while detecting the link


i am using xampp for testing my website and a regular expression to detect links and convert them to a clickable format but when the user enters www.google.com instead of https://www.google.com the link redirects to localhost/www.google.com

my code

function link_detect($text){

  $ex = "/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/i";
  return preg_replace($ex,'<a class="click_link" href="$1" target="_blank">$1</a>', $text);
}

Solution

  • It just because you do not provide http://.

    Try this

    function link_detect($text){
    
      $ex = "/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/i";
      return preg_replace($ex,'<a class="click_link" href="http://$1" target="_blank">$1</a>', $text);
    }
    

    Alternative:

    function text_to_link($str = NULL)
    {
        if($str == '' OR !preg_match('/(http|www\.|@)/i', $str))
        {
            return $str;
        }
     
        $lines      = explode("\n", $str); 
        $return     = '';
        while (list($k,$l) = each($lines)) { 
            $l = preg_replace("/([ \t]|^)www\./i", "\\1http://www.", $l);
            $l = preg_replace("/([ \t]|^)ftp\./i", "\\1ftp://ftp.", $l);
            $l = preg_replace("/(http:\/\/[^ )!]+)/i", "<a href=\"\\1\">\\1</a>", $l);
            $l = preg_replace("/(https:\/\/[^ )!]+)/i", "<a href=\"\\1\">\\1</a>", $l);
            $l = preg_replace("/(ftp:\/\/[^ )!]+)/i", "<a href=\"\\1\">\\1</a>", $l);
            $l = preg_replace("/([-a-z0-9_]+(\.[_a-z0-9-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)+))/i", "<a href=\"mailto:\\1\">\\1</a>", $l);
            $return .= $l."\n";
        }
     
        return $return;
    }
     
     
    /*
    *
    * ------------------------------------------
    * Link
    * <a href="http://www.yours.com">http://www.yours.com</a>
    * <a href="https://www.yours_with_ssl.com">https://www.yours_with_ssl.com</a>
    * ------------------------------------------
    *
    */
    text_to_link('http://yours.com');
    text_to_link('https://yours_with_ssl.com');
     
     
    /*
    *
    * ------------------------------------------
    * FTP
    * <a href="ftp://username:password@yours.com">ftp://username:password@yours.com</a>
    * ------------------------------------------
    *
    */
    text_to_link('ftp://username:password@yours.com');
     
    /*
    *
    * ------------------------------------------
    * Email
    * <a href="mailto:w.kristories@gmail.com">mailto:w.kristories@gmail.com</a>
    * ------------------------------------------
    *
    */
    text_to_link('w.kristories@gmail.com');
    

    UPDATE

    Comment from @Mr.coder

    but if a link allready has a http:// protocol attached to it then what the href would be like http://http://www.google.com

    Ya, update my answer for link_detect().

    function link_detect($text)
    {
      // $ex = "/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/i";
      // return preg_replace($ex,'<a class="click_link" href="http://$1" 
      $ex = preg_replace("/([ \t]|^)www\./i", "\\1http://www.", $text); // Replace www to http://www
      $ex = preg_replace("/(http:\/\/[^ )!]+)/i", "<a target=\"_blank\" href=\"\\1\">\\1</a>", $ex);
      return $ex;
    }
    
    echo link_detect('www.google.com') . "\n";
    echo link_detect('http://google.com') . "\n";
    echo link_detect('http://www.google.com') . "\n";