Auto-link function for PHP.

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
Vael Victus
Forum Newbie
Posts: 24
Joined: Wed Jun 03, 2009 9:29 am

Auto-link function for PHP.

Post by Vael Victus »

I am at the end of my wits. I have scoured Google and all I can find are functions that simply do not work for me.

I'm making a cute, little, forum system. I want the words to auto-link. That means I put the string through my "message convert" function, which also converts the [ b ] stuff to < b >, but none of these work!

Code: Select all

 
function makeURL($URL) {
$URL = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:\+.~#?&//=]+)','<a href=\\1>\\1</a>', $URL);
$URL = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:\+.~#?&//=]+)','<a href=\\1>\\1</a>', $URL);
$URL = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})','<a href=\\1>\\1</a>', $URL);
return $URL;
}
 
That's the best one I have, and it doesn't even turn the http://www.'s into links, and it breaks long links. I have put over an hour into google, I even searched these boards. Nothing.

I'm hoping one of you have the end-all function that will just convert http:// and www. properly.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Auto-link function for PHP.

Post by JAB Creations »

You may want to take a look at this...

viewtopic.php?f=50&t=105483
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Auto-link function for PHP.

Post by John Cartwright »

You must have had some bad search terms. Searched "php regex text to link" on google and found this working example in a few seconds :wink:
Vael Victus
Forum Newbie
Posts: 24
Joined: Wed Jun 03, 2009 9:29 am

Re: Auto-link function for PHP.

Post by Vael Victus »

John Cartwright wrote:You must have had some bad search terms. Searched "php regex text to link" on google and found this working example in a few seconds :wink:
That was just what I needed, thanks so much!

Indeed, I did have poor search terms. It didn't even occur to me to say "regex"... in fact I would never have thought to even put that string in. I was looking for "auto link" or "auto URL".

Thank you also, Creator of JAB, your help was appreciated.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Auto-link function for PHP.

Post by JAB Creations »

No problem...and keep in mind you'll want to minimize your usage of regular expressions when possible. Sometimes they can't be avoided though they are necessary sometimes.
Post Reply