A function to create links.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

A function to create links.

Post by Mightywayne »

Currently I'm using this:

Code: Select all

 
function create_links($message)
{
$message = ereg_replace($message, "(^|[\n ])([\w]+?://[^ ,\"\"\s<]*)", "$1<a href=\"\"$2\"\">$2</a>");
$message = ereg_replace($message, "(^|[\n ])((www|ftp)\.[^ ,\"\"\s<]*)", "$1<a href=\"\"http://$2\"\">$2</a>");
$message = ereg_replace($message, "(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)", "$1<a href=\"\"mailto:$2@$3\"\">$2@$3</a>");
$message = right($message, len($message)-1);
return $message;
}
 
Unfortunately, it's not very good. It doesn't allow for http://'s to be linked without a www, and also, I can't use the function to auto-detect when someone posts a link (in mail, or the forums) which is simply not going to work.

So do any of you know one?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: A function to create links.

Post by RobertGonzalez »

What criteria are you trying to match to actually create a link?
Post Reply