Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 05-01-2010, 06:45 AM Text link into HTML?
Novice Talker

Posts: 9
Name: Bjoern North
Trades: 0
Hi, I searched high and low but couldnt find any tool or code which simply changes plain text links into clickable html links - if anybody knows anything please let me know - thanks in advance
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Bjoern30 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-01-2010, 07:15 AM Re: Text link into HTML?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Maybe I should hold a masterclass on using Search Engines
http://www.google.com/search?hl=&q=p...e+text+to+link

http://www.totallyphp.co.uk/code/con...hyperlinks.htm
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-01-2010, 07:18 AM Re: Text link into HTML?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
This should do it:
PHP Code:
$text 'blah blah http://google.com blah blah blah blah';

echo 
preg_replace('/(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~\/|\/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:\/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|\/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?/i''<a href="$0">$0</a>'$text); 
Edit: Chris beat me to it

The function on the page he linked to uses ereg functions, which are deprecated. Here is a version using preg_replace instead:

PHP Code:
function makeClickableLinks($text) { 

  
$text preg_replace('/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i'
    
'<a href="\\1">\\1</a>'$text); 
  
$text preg_replace('/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i'
    
'\\1<a href="http://\\2">\\2</a>'$text); 
  
$text preg_replace('/([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/i'
    
'<a href="mailto:\\1">\\1</a>'$text); 
   
return 
$text

}

$text 'blah blah http://google.com blah blah';

echo 
makeClickableLinks($text); 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 05-01-2010 at 07:25 AM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 05-01-2010, 09:08 AM Re: Text link into HTML?
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
The huge OR statement in the regular expression is not needed (com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|m useum|travel|), and is inadequate. I already see several that are missing, and after all, you never know when new top-level domains will be added.

Here's the one I last used:
Code:
@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to Text link into HTML?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.36340 seconds with 12 queries