I am using a PHP function to automatically turn URLs in a text string into an actual link that people can click on. It seems to work in most cases, however I have found some cases where it does not.
I don't really understand regular expressions at all, so I was hoping someone could help me out with this.
Here is the pattern I'm currently using:
$pattern = "/(((http[s]?:\/\/)|(www\.))(([a-z][-a-z0-9]+\.)?[a-z][-a-z0-9]+\.[a-z]+(\.[a-z]{2,2})?)\/?[a-z0-9.,_\/~#&=;%+?-]+[a-z0-9\/#=?]{1,1})/is";
However here are some links I have found that this pattern is not matching:
Does anyone know how I can modify that regex pattern to match these cases as well?
EDIT - It should also match URLs that may have a period at the end. If a URL is the last part of a sentence there may be a period at the end that should not be included in the actual link. Currently this pattern takes that into account as well.
EDIT 2 - I am using the pattern like this:
$pattern = "/((http|https):\/\/)?([a-z0-9-]+\.)?[a-z][a-z0-9-]+(\.[a-z]{2,6}){1,3}(\/[a-z0-9.,_\/~#&=;%+?-]*)?/is";
$string = preg_replace($pattern, " <a target='_blank' href='$1'>$1</a>", $string);
// fix URLs without protocols
$string = preg_replace("/href='www/", "href='http://www", $string);
return $string;
The following regex will match URLs:
http://
or https://
www.example.com
, help.example.com
, etc)www.example.com.gu
, www.example.com.au.museum
, etc)The /i
at the end makes it case insensitive.
/((http|https):\/\/)?([a-z0-9-]+\.)?[a-z0-9-]+(\.[a-z]{2,6}){1,3}(\/[a-z0-9.,_\/~#&=;%+?-]*)?/is
Edit: This will not match any "hanging" periods at the end (such as the end of a sentence) because it's not part of the URL, and shouldn't be included in the href
attribute of your link.
Edit 2: In your first preg_replace()
, change $1
to $0
. This will insert the entire matched string instead of a single part of it.
Edit 3: (Update 2) Here's a better way you can check for a http://
or https://
at the beginning:
preg_replace("/href='[^h][^t][^t][^p][^s]?[^:]/", "/href='http:\/\/", $string);
I had problems with all the examples above.
Here is one that works:
function autolink($string){
$string= preg_replace("#http://([\S]+?)#Uis", '<a href="http://\\1">\\1</a>', $string);
return $string;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With