Create link script/ Parse URL, email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vodius
Forum Newbie
Posts: 6
Joined: Sun Apr 06, 2008 4:21 pm

Create link script/ Parse URL, email

Post 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
vodius
Forum Newbie
Posts: 6
Joined: Sun Apr 06, 2008 4:21 pm

Re: Create link script/ Parse URL, email

Post 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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Create link script/ Parse URL, email

Post 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.
vodius
Forum Newbie
Posts: 6
Joined: Sun Apr 06, 2008 4:21 pm

Re: Create link script/ Parse URL, email

Post 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
aCa
Forum Newbie
Posts: 11
Joined: Sun Apr 06, 2008 3:43 pm

Re: Create link script/ Parse URL, email

Post 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 :-)
vodius
Forum Newbie
Posts: 6
Joined: Sun Apr 06, 2008 4:21 pm

Re: Create link script/ Parse URL, email

Post by vodius »

Wow that's way easier! Thanks!
geethalakshmi
Forum Commoner
Posts: 31
Joined: Thu Apr 24, 2008 10:38 pm

Re: Create link script/ Parse URL, email

Post by geethalakshmi »

Hi,
The link given below might help you regarding your doubt.
http://hiox.org/bypass/index.php?id=200
Post Reply