Page 1 of 1

Solved: Regular expression for url

Posted: Fri Sep 28, 2012 8:41 am
by mister_v
Hi,

I would like to match all url's find in a text.
with preg_replace().

Can anyone give me the perfect pattern to math them?

Thanks

Re: Regular expression for url

Posted: Fri Sep 28, 2012 12:00 pm
by chris
I find the following pattern works well for me
"@((https?://|ftp://)([-\w\.]+)+(:\d+)?(/([\w/_\.\-\+\~\=\:]*(\?\S+)?)?)?)@"

Code: Select all

$pattern=array("@((https?://|ftp://)([-\w\.]+)+(:\d+)?(/([\w/_\.\-\+\~\=\:]*(\?\S+)?)?)?)@");
$replace=array("[Somtehing]\\1");
$str=preg_replace($pattern,$replace,$string);

Re: Regular expression for url

Posted: Fri Sep 28, 2012 12:44 pm
by chris
On this site you get a good overview of several patterns,
with there pro's and con's:
http://mathiasbynens.be/demo/url-regex

Re: Solved: Regular expression for url

Posted: Mon Nov 12, 2012 8:26 pm
by mister_v
Thanks, this helps me a lot.