Welcome to Geeklog, Anonymous Friday, July 12 2024 @ 03:56 pm EDT

Geeklog Forums

Automated href tag

Status: offline


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

is there a way to integrate some automated href substitution so that somesting like:
is allways converted to

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

Status: offline


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?

Status: offline


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


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


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.

All times are EDT. The time is now 03:56 pm.

  • 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