Jump to content

finding urls


taith

Recommended Posts

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

<?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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.