Automatic Text to 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
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Automatic Text to Links?

Post by qads »

hi,
how can i turn all the urls/website addresses in $block_from_db into clickble links?

Code: Select all

<?php $block_from_db = "today www.site.com had blah blah with www.site-com.com";
print("$block_from_db");?>
like this forum turns any link into a clickble link, .e.g. http://www.somesite.com

i have no idea how to do it, i have searched quite alot of places but have't found anything for this.

please help
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

Code: Select all

$block_from_db = preg_replace("/((http:\/\/)|(www\.))(&#1111;\S\.]+)\b/i", "&lt;a href="http://$3$4$5"&gt;$2$3$4$5&lt;/a&gt;", $block_from_db);
Last edited by dusty on Tue Sep 10, 2002 9:22 pm, edited 1 time in total.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

$data = eregi_replace('(&#1111;&#1111;:space:]()&#1111;&#123;&#125;])(www.&#1111;-a-zA-Z0-9@:%_\+.~#?&//=]+)',
   '\\1<a href="http://\\2" target="_blank">\\2</a>', $data);

   $data = eregi_replace('(&#1111;&#1111;:space:]()&#1111;&#123;&#125;])(http://&#1111;-a-zA-Z0-9@:%_\+.~#?&//=]+)',
  '\\1<a href="\\2" target="_blank">\\2</a>', $data);
does the trick for me.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

I get easily confuzed when I see the /^#$^3461/234!@%25 stuff. :oops:
Image Image
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

it's just a list of characters that can be used in URL's
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Try looking http://www.phpbuilder.com/columns/dario19990616.php3 about Regular Expression. I think PREG does it faster than EREG.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

very nice :D , thanks alot!
Post Reply