Forum Moderators: coopster

Message Too Old, No Replies

looking for opposite of strip_tags

I want to change urls in text into clickable links

         

gilahacker

1:10 am on Mar 24, 2005 (gmt 0)

10+ Year Member



I know this can be done by using the wsiwyg editors but I don't want/need all that functionality.

All I want is that when someone inputs "visit this great site: [webmasterworld.com"...] that it automatically makes it a link.

It can do it either when it's being entered into the database or when it's being displayed on the site.

Webmasterworld does this somehow but only on links with "http://" at the beginning, I'd want to do it if the http were there or not...

i.e.

<?php
echo add_hrefs($displaytext); //outputs text and adds a hrefs
?>

and we'd just have to make some kind of function and name it add_hrefs.

or maybe I'm going about this all wrong I don't know...

Anyone have any suggestions?

Thanks,
-Jason

iceman22

2:05 am on Mar 24, 2005 (gmt 0)

10+ Year Member



If you want it as a function you could write it like this:

add_hrefs($text) {

$text = preg_replace('/(?<!=)(?<!])(http¦ftp)+(s)?:(\/\/)((\w¦\.)+)(\/)?(\S+)?/i','<a href="\0">\0</a>', $text);

return $text;

}

Alternatively, you could use the use the preg_replace regular expression as it is.

ironik

3:20 am on Mar 24, 2005 (gmt 0)

10+ Year Member



I got this one from a free reference somewhere, I forget where:


function urlify($html, $target)
{
// Look for valid address in a block of text,
// Convert it to a html url (watching for punctuation or end of string)
$pattern ="{\\b((https?¦telnet¦gopher¦file¦wais¦ftp) : [\\w/\\#~:.?+=&%@!\\-]+?) (?= [.:?\\-]* (?:[^\\w/\\#~:.?+=&%@!\\-] ¦$) ) }x";
return preg_replace ($pattern, "<a href=\"$1\" target=\"" . $target . "\">$1</a>", $html);
}

It'll only work with functions prepended by the transfer protocol (not quite what you wanted), but it is quite accurate in picking out a proper URL.

gilahacker

9:08 am on Mar 24, 2005 (gmt 0)

10+ Year Member



thanks guys, guess I forgot to check the "email notification" box... just happened to poke back in here

I'll try these out

Thanks again,

-Jason

gilahacker

10:18 pm on Mar 24, 2005 (gmt 0)

10+ Year Member



#iceman22

I tried this:

$text="hello, welcome to wwWebmasterWorldebsite.com";
$text = preg_replace('/(?<!=)(?<!])(http¦ftp)+(s)?:(\/\/)((\w¦\.)+)(\/)?(\S+)?/i','<a href="\0">\0</a>', $text);
echo $text . "<br><br><br>";
$text="hello, welcome to [wwWebmasterWorldebsite.com";...]
$text = preg_replace('/(?<!=)(?<!])(http¦ftp)+(s)?:(\/\/)((\w¦\.)+)(\/)?(\S+)?/i','<a href="\0">\0</a>', $text);
echo $text;

but it didn't seem to do anything, just output the text, no links.

I'm going to try the other method listed

gilahacker

10:19 pm on Mar 24, 2005 (gmt 0)

10+ Year Member



okay, the filter screwed up the mywebsite dot com I had in there...

iceman22

4:36 am on Mar 25, 2005 (gmt 0)

10+ Year Member



There are many many ways or parsing URLs to hyperlink them.

If you want to do this to links that do not have http:// preceding them, then it gets a lot more complicated as URLs can start with www. or just the domain, the only constant would the be domain suffix, and there are now many of them.

gilahacker

6:23 pm on Mar 25, 2005 (gmt 0)

10+ Year Member



I'm honestly thinking of just having another field for them to input their website, and then just listing the link to their website at the bottom of the "about the author" box...