Code: Select all
$message = preg_replace('#'
. '(http://)?'
. '('
. '(([0-9a-z_!~*\'().&=+$%-]+: )?[0-9a-z_!~*\'().&=+$%-]+@)?' //user@
. '(([0-9]{1,3}\.){3}[0-9]{1,3}' // IP- 199.194.52.184
. '|' // allows either IP or domain
. '([0-9a-z_!~*\'()-]+\.)*' // tertiary domain(s)- www.
. '([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.' // second level domain
. '[a-z]{2,6})' // first level domain- .com or .museum
. '(:[0-9]{1,4})?' // port number- :80
. '((/?)|' // a slash isn't required if there is no file name
. '((/[0-9a-z_!~*\'().;?:@&=+$,%\#-]+)+/?)*?)'
. ')#i',
'<a href="http://$2" target="_blank">http://$2</a>',
$message
);It's matching these:
Code: Select all
http://www.example.com
www.example.com
example.com
www.example.com/
http://example.com/
..and so on...Code: Select all
example.com/folder
makes:
<a href="http://example.com/">http://example.com/</a>folderCode: Select all
//change this line:
. '((/?)|' // a slash isn't required if there is no file name
//to this:
. '((/?)' // a slash isn't required if there is no file nameCode: Select all
example.com/folder <<works
example.com/ <<doesn'tI need something that's definitely going to work beautifully. If someone, say, does this:
Code: Select all
http://sub.example.com/blah/howdy.php?x=2&y=a%20b#hereI only need http, though. ftp and whatnot I can leave out. It would be nice to add in https, but I can't seem to figure that out either. I mean, yeah, I can find it. Great. Problem is..
if http or https is typed, use it. else, insert http.
Which I translate to splitting it up into more than one regex.
I've tried that and failed miserably.
I need this to be as generic as possible--in other words, work on everything.
Anyone have a simpler solution?
By the way--I just realized