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? Link to comment https://forums.phpfreaks.com/topic/51851-finding-urls/ 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. Link to comment https://forums.phpfreaks.com/topic/51851-finding-urls/#findComment-255580 Share on other sites More sharing options...
taith Posted May 17, 2007 Author Share Posted May 17, 2007 wow... sweet THANKS! Link to comment https://forums.phpfreaks.com/topic/51851-finding-urls/#findComment-255725 Share on other sites More sharing options...
Wildbug Posted May 17, 2007 Share Posted May 17, 2007 No problem! GrammAr, rather. Link to comment https://forums.phpfreaks.com/topic/51851-finding-urls/#findComment-255757 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.