سؤال

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);
}
هل كانت مفيدة؟

المحلول 2

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";

نصائح أخرى

Actually, this less about your regex and more about your link.

In anchors, the only way for the browser to know that links refer to a site elsewhere on the internet is to provide a URI. http:// denotes a URI (with the scheme http).

Remember that on most filesystems a file can have multiple dots, so when you say go to www.google.com the browser is going to think you mean a file called www with the extension .google.com is where you want to go. It's no different than foo.tar.gz.

If anything, prepend just a //, which in browser-terms means prepend whatever protocol/scheme this page has (http/https/file) to the URL and treat as an external link.

Translating this to Regex

One possible solution is to try to detect (((https?|file):)?\/\/)?, extract the 4th group from that (https?|file, which will find either http, https, or file), and then prepend it to the beginning of your link, always providing //.

That way, if no scheme is specified the link will still tell the browser to use a default scheme of whatever the current page is using and that the link is external.

((((?:([A-Za-z]{3,9}):)?(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)

Group 4 now holds the protocol (i.e. http:). Prepend whatever is in that to your link with // after it (always).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top