Page 1 of 1

Create link script/ Parse URL, email

Posted: Sun Apr 06, 2008 4:44 pm
by vodius
Hi,
This might sound confusing but i'll try to explain the best I can.
I'm using Noah's Classifieds system on my website and want to expand it a bit. I have description outputted from database and HTML code being filtered using htmlspecialchars function. However I want to keep URLs' and email's links active, i.e. keep htmlspecialchars function and include another function, let's say I call it enableLink() that will process description value to avoid enabling other HTML tags like table, div etc.
Example:
Description is:
A Real Business For Real People Serious About Getting Real Results. CEO Income From Home.
http://retirewithroyal.com
My board outputs without url being clickable: http://bcfreeclassifieds.com/index.php? ... owhtmllist

I want script to find http:// or www. and replace them to include <a href=" URL " taget="_blank">URL goes here</a>
Same for email.
I cannot change value in database because if I add <a href ... >code it will output it as string using html ascii codes.

I see that this board has some kind of script like this.
Does it make sense?

Any help is appreciated!
Anton

Re: Create link script/ Parse URL, email

Posted: Mon Apr 07, 2008 1:34 am
by vodius
Never mind, figured using function:

Code: Select all

function formatHyperlink($para1) 
{
    $para1 = str_replace("<br />", " <br /> ", $para1);
    $para1_arr = explode(' ',$para1);
    $i = 0;
    foreach ($para1_arr as $value) {
        $newarr[$i] = $value;
        if ((strstr($value, 'www.') != FALSE) || (strstr($value, 'http://') !=FALSE)) {
            if (strstr($value, 'http://') !=FALSE) {
                $value = str_replace("http://", "", $value);
            }
            $value = "<a href='http://$value' target='_blank'>$value</a>";
            $newarr[$i] = $value;
        }
        if (strstr($value, '@') !=FALSE) {
            $value = "<a href='mailto:$value'>$value</a>";
            $newarr[$i] = $value;
        }
        $i = $i + 1;
    }
    $para1 = implode(' ', $newarr);
    return($para1);
}
Any suggestions if it could be done more efficiently?
Thanks

Re: Create link script/ Parse URL, email

Posted: Mon Apr 07, 2008 10:11 am
by Jonah Bron
If I understand correctly, you should be able to use strip_tags

Code: Select all

$output = strip_tags($output, '<a>');
That will strip all tags except for the anchor tag.

Re: Create link script/ Parse URL, email

Posted: Mon Apr 07, 2008 10:14 am
by vodius
Strip the tag? I do not have any tags in the text, I want to make <a> tags by recognizing web urls and email addresses and make them into active hyperlinks.
Thanks

Re: Create link script/ Parse URL, email

Posted: Mon Apr 07, 2008 12:41 pm
by aCa
You could do some preg to solve the problem.

Code: Select all

 
$string = 'Test with www and http:// and @ http://www.domain1.com and http://domain2.com and http://www.domain3.com and mail@domain.com'
 
preg_replace(array('/(http:\/\/[^\s]+)/', '/(www\.[^\s]+)/', '/([^\s]+@[^\s]+\.[a-z]+)/i'), array('<a href="$1" target="_blank">$1</a>', '<a href="http://$1" target="_blank">$1</a>', '<a href="mailto:$1">$1</a>'), $string);
 
You could do more exact validation for the e-mail but I just did a quick pattern. To test the patterns feel free to try my regex tool :-)

Re: Create link script/ Parse URL, email

Posted: Mon Apr 07, 2008 1:17 pm
by vodius
Wow that's way easier! Thanks!

Re: Create link script/ Parse URL, email

Posted: Thu Apr 24, 2008 11:42 pm
by geethalakshmi
Hi,
The link given below might help you regarding your doubt.
http://hiox.org/bypass/index.php?id=200