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
Create link script/ Parse URL, email
Moderator: General Moderators
Re: Create link script/ Parse URL, email
Never mind, figured using function:
Any suggestions if it could be done more efficiently?
Thanks
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);
}Thanks
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Create link script/ Parse URL, email
If I understand correctly, you should be able to use strip_tags
That will strip all tags except for the anchor tag.
Code: Select all
$output = strip_tags($output, '<a>');Re: Create link script/ Parse URL, email
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
Thanks
Re: Create link script/ Parse URL, email
You could do some preg to solve the problem.
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 
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);
Re: Create link script/ Parse URL, email
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
Hi,
The link given below might help you regarding your doubt.
http://hiox.org/bypass/index.php?id=200
The link given below might help you regarding your doubt.
http://hiox.org/bypass/index.php?id=200