General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 14th, 2003, 01:01 AM
DDDooGGG DDDooGGG is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Melbourne, Australia
Posts: 97 DDDooGGG User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 33 sec
Reputation Power: 6
Question Replace http:// to a URL?

What do i have to do, to do the following:
if somebody pastes a url i.e. http://this address.com, how can i make it into a clickable link when the user submits the form and the message gets displayed on a new page.

I am using the following, but it makes the whole string a link.
PHP Code:
 eregi_replace("http://","<a href=\"$str\">$str</a>",$str); 
__________________
regards,


Fulton

Reply With Quote
  #2  
Old September 14th, 2003, 02:13 AM
numbernine numbernine is offline
Up To His Eyes In Ads
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Chicago
Posts: 160 numbernine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 25 sec
Reputation Power: 7
You'll want to use a regular expression. Here's one that I found on the web for this purpose.

Code:
        $output = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "<a href=\"\\1://\\2\\3\" target=\"_blank\" target=\"_new\">\\1://\\2\\3</a>", $strText);

Reply With Quote
  #3  
Old September 14th, 2003, 04:35 AM
DDDooGGG DDDooGGG is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Melbourne, Australia
Posts: 97 DDDooGGG User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 33 sec
Reputation Power: 6
Thanks for your help i tried the following as a test, but its not working. any ideas?

PHP Code:
 dohttp($strText){
 
$output eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])""<a href=\"\\1://\\2\\3\" target=\"_blank\" target=\"_new\">\\1://\\2\\3</a>"$strText);
    return(
$output);

$string "http://www.link_one.com this is a test";
echo 
dohttp($string); 

Reply With Quote
  #4  
Old September 15th, 2003, 10:17 AM
numbernine numbernine is offline
Up To His Eyes In Ads
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Chicago
Posts: 160 numbernine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 25 sec
Reputation Power: 7
Sorry! I only gave you half of the function

Code:
<?
  function createHTML($strText) {
        $output = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "<a href=\"\\1://\\2\\3\" target=\"_blank\" target=\"_new\">\\1://\\2\\3</a>", $strText);
        $output = eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))", "<a href=\"mailto:\\1\" target=\"_new\">\\1</a>", $output);
        $output = nl2br($output);
        return $output;
  }
$string = "http://www.link_one.com this is a test";
echo createHTML($string);
?>

Reply With Quote
  #5  
Old September 15th, 2003, 10:34 PM
DDDooGGG DDDooGGG is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Melbourne, Australia
Posts: 97 DDDooGGG User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 33 sec
Reputation Power: 6
Hi, thanks for that it works great, now i need to differentiate between bbcode and a normal http: link.

my code is below.

PHP Code:
// Parse BB code
function bbcode($str){ 
        
$str preg_replace('/(\[b\])(.+?)(\[\/b\])/i''<b>\\2</b>',$str); 
        
$str preg_replace('/(\[i\])(.+?)(\[\/i\])/i''<i>\\2</i>',$str); 
        
$str preg_replace('/(\[u\])(.+?)(\[\/u\])/i'"<u>\\2</u>"$str);  
        
$str preg_replace('/(\[quote\])(.+?)(\[\/quote\])/i'"<font color=\"660000\">\\2 </font>"$str); 
        
$str preg_replace('/(\[color=(.+?)\])(.+?)(\[\/color\])/i''<font color=\\2>\\3</font>',$str);        
         
$str preg_replace('/(\[image\])(.+?)(\[\/image\])/i'"<img src=\"\\2\">"$str);
        
// Parses web links if not bbcode
        
if (substr($str,07) == "http://"){
            
$str eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])""<a href=\"\\1://\\2\\3\" target=\"_blank\" target=\"_new\">\\1://\\2\\3</a>"$str);
        } else {
            
$str preg_replace('/(\[url\])(.+?)(\[\/url\])/i'"<a href=\"http://\\2\" target=\"_blank\">\\2</a>"$str); 
            
$str preg_replace('/(\[url=\])(.+?)(\[\/url\])/i'"<a href=\"http://\\2\" target=\"_blank\">\\2</a>"$str); 
            
$str preg_replace('/(\[url=(.+?)\])(.+?)(\[\/url\])/i'"<a href=\"http://\\2\" target=\"_blank\">\\3</a>"$str); 
        }
        
//Parses email links , if not bbcode
        
if (substr($str,07) == "mailto:"){
            
$str eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))""<a href=\"mailto:\\1\" target=\"_new\">\\1</a>"$str);
        } else{
            
$str preg_replace('/(\[email\])(.+?)(\[\/email\])/i'"<a href=\"mailto:\\2\">\\2 </a>"$str); 
            
$str preg_replace('/(\[email=(.+?)\])(.+?)(\[\/email\])/i'"<a href=\"mailto:\\2\">\\3</a>"$str); 
        }
return 
$str

$string "[url]http://www.linkone.com[/url] this is 
[url]http://www.linktwo.com[/url]
a test [email]fulton@dynamicart.net[/email]"
;
echo 
nl2br(bbcode($string)); 


the function need to be able to check whether each word begins with http or [url] and then do the correct thing.

Reply With Quote
  #6  
Old April 4th, 2004, 02:10 PM
Spongy's Avatar
Spongy Spongy is offline
Alternately High
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Hilversum, Netherlands
Posts: 223 Spongy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 56 m 41 sec
Reputation Power: 5
Send a message via MSN to Spongy
Doesn't it do that yet?
I haven't checked your function yet, I will implement it myself and test it.
It looks good though...

Cheers,
__________________
Work to live, don't live to work

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Replace http:// to a URL?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT