taith Posted May 17, 2007 Share Posted May 17, 2007 trying to make something like this page's... takes any url entered and like http://www.google.com and puts in the <a>'s i've gotten this sofar... but i'm regex incompetant... function autolink($string){ preg_match('@^(?:http://www.)@i',$string, $matches); print_r($matches); return $string2; } echo autolink('test http://www.google.ca test'); any pointers? Quote Link to comment Share on other sites More sharing options...
Wildbug Posted May 17, 2007 Share Posted May 17, 2007 <?php $pattern = '<((?:http|ftp)://[^ \'"]+[A-Z0-9/]\b)>i'; $url_in_text = 'test http://www.google.ca test ftp://ftp.rfc-editor.org/in-notes/rfc3986.txt.'; echo preg_replace($pattern,'<a href="$1">$1</a>',$url_in_text); ?> You might be better off reading the RFC for the URI spec, but the above will get you close. It matches either http or ftp URIs and anything that's not a space or quotation mark, but is terminated by an alphanumeric digit or a slash. If you're searching through text and come across a URL at the end of a sentence, this pattern won't include the terminating punctuation, which is a grammer device, not part of the URL. Quote Link to comment Share on other sites More sharing options...
taith Posted May 17, 2007 Author Share Posted May 17, 2007 wow... sweet THANKS! Quote Link to comment Share on other sites More sharing options...
Wildbug Posted May 17, 2007 Share Posted May 17, 2007 No problem! GrammAr, rather. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.