Welcome to Geeklog, Anonymous Thursday, March 28 2024 @ 09:45 am EDT

Geeklog Forums

Automated href tag


Status: offline

narres

Forum User
Newbie
Registered: 11/14/03
Posts: 3
Hi Geeks,

is there a way to integrate some automated href substitution so that somesting like:
http://linux.24x.info/news/
is allways converted to
http://linux.24x.info/?

I know to do it whith:
$text = ereg_replace("[[:alpha:]]+://([^[:space:]]+[[:alnum:]/])","\1", $text);
But how integrate this into the template for {text_body}?
 Quote

Status: offline

Turias

Forum User
Full Member
Registered: 10/20/03
Posts: 807
Well, do you want to do the replace on submission or when it is displayed? And do you want it to happen for stories? Forum posts? Events? Everything?
 Quote

Status: offline

pibby

Forum User
Newbie
Registered: 12/23/03
Posts: 4
curious
I am also very interested in something like this for stories. Where would I put that code?Has anyone tried it?
 Quote

FreakWorld

Anonymous
Ya this will be great for stories.
I keep getting people that don't know basic html submitting stories with urls. And thin I have to go through the hole thing and fix it. This will be cool if someone figures it out.

(I bet thats whats somone is thinking about this post. You don't know basic php and now we have to go and fix it.) Laughing Out Loud
 Quote

rakaz

Anonymous
I use the following function for converting URLs plain text to clickable links. I run this function on the submitted text of a comment before saving it in the database, so this function isn't run everytime somebody views the comment.

You could probably insert something like this also on the submission pages of stories and other user submitted text.

Text Formatted Code
function COM_convertURLs ( $text )
{
        //  First match things beginning with http:// (or other protocols)
        $NotAnchor = '(?<!"|href=|hrefs=s|href=s|hrefs=)';
        $Protocol = '(http|ftp|https)://';
        $Domain = '[w]+(.[w]+)';
        $Subdir = '([w-.;,@?^=%&:/~+#]*[w-@?^=%&/~+#])?';
        $Expr = '/' . $NotAnchor . $Protocol . $Domain . $Subdir . '/i';

        $text = preg_replace( $Expr, "<a href="$0" target="_blank">$0</a>", $text );
       
        //  Now match things beginning with www.
        $NotAnchor = '(?<!"|-|href=|hrefs=s|href=s|hrefs=)';
        $NotHTTP = '(?<!://)';
        $Domain = 'www(.[w]+)';
        $Subdir = '([w-.;,@?^=%&:/~+#]*[w-@?^=%&/~+#])?';
        $Expr = '/' . $NotAnchor . $NotHTTP . $Domain . $Subdir . '/i';

        return preg_replace( $Expr, "<a href="http://$0" target="_blank">$0</a>", $text );     
}
 


This function is based on an example found on php.net, but modified to make it handle certain exceptions better.
 Quote

All times are EDT. The time is now 09:45 am.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content