Status: offline

narres

Forum User
Newbie
Registered: 14/11/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}?

Status: offline

Turias

Forum User
Full Member
Registered: 20/10/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

pibby

Forum User
Newbie
Registered: 23/12/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?

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.)

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.

PHP 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.