Regular Expressions 101

Community Patterns

url to link

0

Regular Expression
PCRE (PHP <7.3)

/
(?:http(s)?:\/\/(*SKIP))?(?(?<=(?::\/\/))(?:www\.)?|(?:www\.))((?:[\w-]+(?:\.[a-zA-Z0-9]{2,4}[\/?]?)))([\w\d-=+&\/]*)
/
g

Description

The fastest I could come up with to find links in a text story to convert to html tag for web use Most make links out of garbage like ss.ss.sss even www.www this requires www if it isnt following https?:// so it finds urls not typos IF your paranoid that it might pull links out of an html tag in your raw text add this to the beginning (?:<a[^<]+</a>)(*SKIP)(*FAIL)|

And yes I did use a base url for the link text on purpose because www.whatever.com is easier to read than www.whatever.com/manythings.php?annoying=this&mostpeople=wontUnderstandAllThis&plus=ItsHardToReadInAstory

Submitted by mikesbored - 4 years ago