8

following code is used to find url from a string with php. Here is the code:

$string = "Hello http://www.bytes.com world www.yahoo.com";
preg_match('/(http:\/\/[^\s]+)/', $string, $text);
$hypertext = "<a href=\"". $text[0] . "\">" . $text[0] . "</a>";
$newString = preg_replace('/(http:\/\/[^\s]+)/', $hypertext, $string);
echo $newString;

Well, it shows a link but if i provide few link it doesn't work and also if i write without http:// then it doesn't show link. I want whatever link is provided it should be active, Like stackoverflow.com.

Any help please..

Aerendir
  • 6,152
  • 9
  • 55
  • 108
user1161867
  • 141
  • 1
  • 1
  • 11
  • 3
    possible duplicate of [PHP: Regular Expression to get a URL from a string](http://stackoverflow.com/questions/2720805/php-regular-expression-to-get-a-url-from-a-string) – Gordon Feb 05 '12 at 18:16

3 Answers3

17

A working method for linking with http/https/ftp/ftps/scp/scps: $newStr = preg_replace('!(http|ftp|scp)(s)?:\/\/[a-zA-Z0-9.?&_/]+!', "<a href=\"\\0\">\\0</a>",$str);

I strongly advise NOT linking when it only has a dot, because it will consider PHP 5.2, ASP.NET, etc. links, which is hardly acceptable.

Update: if you want www. strings as well, take a look at this.

axiomer
  • 2,088
  • 1
  • 17
  • 26
  • 1
    well @axiomer, but it's not active if i write www.example.com – user1161867 Feb 05 '12 at 18:26
  • Updated my solution with a link (I didn't test it but it should work, I think). – axiomer Feb 05 '12 at 18:32
  • 1
    This doesn't work if the URL has a hyphen (-). The link ends before the hyphen. – doubleJ Nov 27 '13 at 02:24
  • this worked for me but I noticed that when I linked youtube videos it broke the link at the `=` sign so I modified it to work with youtube video links `$newStr = preg_replace('!(http|ftp|scp)(s)?:\/\/[a-zA-Z0-9.?&_/=]+!', "\\0",$str);` – Cesar Bielich Oct 27 '14 at 20:44
  • 1
    Updated code to make it work when the URL has a hyphen (`-`), `%` or `=` sign: `$newStr = preg_replace('!(http|ftp|scp)(s)?:\/\/[a-zA-Z0-9.?%=\-&_/]+!', "\\0",$str);` – Mukesh Chapagain Apr 14 '15 at 03:23
3

If you want to detect something like stackoverflow.com, then you're going to have to check for all possible TLDs to rule out something like Web 2.0, which is quite a long list. Still, this is also going to match something as ASP.NET etc.

The regex would looks something like this:

$hypertext = preg_replace(
    '{\b(?:http://)?(www\.)?([^\s]+)(\.com|\.org|\.net)\b}mi',
    '<a href="http://$1$2$3">$1$2$3</a>',
    $text
);

This only matches domains ending in .com, .org and .net... as previously stated, you would have to extend this list to match all TLDs

bummzack
  • 5,805
  • 1
  • 26
  • 45
  • This only works for the domain portion. If the string includes directories and/or files, the link will end before those. – doubleJ Nov 27 '13 at 02:26
0

@axiomer your example wasn't work if link will be in format:

https://stackoverflow.com?val1=bla&val2blablabla%20bla%20bla.bl

correct solution:

preg_replace('!(http|ftp|scp)(s)?:\/\/[a-zA-Z0-9.?%=&_/]+!', "<a href=\"\\0\">\\0</a>", $content);

produces:

<a href="https://stackoverflow.com?val1=bla&val2blablabla%20bla%20bla.bl">https://stackoverflow.com?val1=bla&val2blablabla%20bla%20bla.bl</a>
Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65