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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.