Author Topic: Displaying hyperlinks...  (Read 464 times)

0 Members and 1 Guest are viewing this topic.

Offline markvaughn2006Topic starter

  • Enthusiast
  • Posts: 105
    • View Profile
Displaying hyperlinks...
« on: January 31, 2010, 10:48:26 AM »
sorry for being such  a noob.. but how do i echo a users input and allow hyperlinking?? Right now if a person puts http://example.com somewhere in their message you can't click on it...thanks for any help  ::)

Offline premiso

  • Karma Chameleon
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,670
  • Gender: Female
  • effing right
    • View Profile
    • PHP Help
Re: Displaying hyperlinks...
« Reply #1 on: January 31, 2010, 10:57:00 AM »
Just found this at the preg_replace() man.

function clickable($url){
        
$in=array(
        
'`((?:https?|ftp)://\S+[[:alnum:]]/?)`si',
        
'`((?<!//)(www\.\S+[[:alnum:]]/?))`si'
        
);
        
$out=array(
        
'<a href="$1"  rel=nofollow>$1</a> ',
        
'<a href="http://$1" rel=\'nofollow\'>$1</a>'
        
);
        return 
preg_replace($in,$out,$url);
    }


May be what you are looking for.

Offline markvaughn2006Topic starter

  • Enthusiast
  • Posts: 105
    • View Profile
Re: Displaying hyperlinks...
« Reply #2 on: January 31, 2010, 12:29:18 PM »
Ok, just gotta post this, this has got to be the easiest least frustrating way to do this..
<?php  function makeClickableLinks($text) {    $text eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',     '<a href="\\1">\\1</a>'$text);   $text eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',     '\\1<a href="http://\\2">\\2</a>'$text);   $text eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',     '<a href="mailto:\\1">\\1</a>'$text);    return $text;  }

much easier than i expected!

Offline markvaughn2006Topic starter

  • Enthusiast
  • Posts: 105
    • View Profile
Re: Displaying hyperlinks...
« Reply #3 on: January 31, 2010, 12:30:53 PM »
sorry .... here it is
<?php  function makeClickableLinks($text) {    $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',     '<a href="\\1">\\1</a>', $text);   $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',     '\\1<a href="http://\\2">\\2</a>', $text);   $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',     '<a href="mailto:\\1">\\1</a>', $text);    return $text;  }?>

to use - echo makeClickableLinks($text);

messy but you can figure it out

Offline premiso

  • Karma Chameleon
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,670
  • Gender: Female
  • effing right
    • View Profile
    • PHP Help
Re: Displaying hyperlinks...
« Reply #4 on: January 31, 2010, 12:34:56 PM »
The problem with that function is eregi() is depreciated in PHP6. It is advised to use preg() functions instead, as they are not being depreciated.

Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: Displaying hyperlinks...
« Reply #5 on: February 01, 2010, 06:06:35 AM »
Technically eregi is already deprecated (as of PHP 5.3) and will be removed as of PHP 6.0, but premisos' point still holds true. That being said it wouldn't take much to convert it to use preg_replace().
CodeCanyon - Cheap, High Quality, Ready Made Scripts.