ellandrd
asked on
remove HTML tags from href="" values
i have this script to find website addresses in a string and wrap <a> tags around it. however if the website address is at the end of the string it appends the </p> tags to the href="" value like this:
<p><a href="http://www.site.com</p>" title="">http://www.site.com</p></a>
<p><a href="http://site.com/contact.html</p>" title="">http://site.com/contact.html</p></a>
my code:
<?php
function fcnConvertWebsiteAddress($ data)
{
$data = preg_replace('/(https?\:\/ \/\S*)/',' <a href="${1}" title="">${1}</a>',$data);
return preg_replace('/(?<![\'"])( [^\/])(www \.[^\s\,"] +)/','$1<a href="$2" title="">$2</a> ', $data);
}
$data = '<p>http://www.site.com</p>'."\n";
$data .= '<p>http://site.com/contact.html</p>'."\n";
$data = fcnConvertWebsiteAddress($ data);
echo $data;
?>
<p><a href="http://www.site.com</p>" title="">http://www.site.com</p></a>
<p><a href="http://site.com/contact.html</p>" title="">http://site.com/contact.html</p></a>
my code:
<?php
function fcnConvertWebsiteAddress($
{
$data = preg_replace('/(https?\:\/
return preg_replace('/(?<![\'"])(
}
$data = '<p>http://www.site.com</p>'."\n";
$data .= '<p>http://site.com/contact.html</p>'."\n";
$data = fcnConvertWebsiteAddress($
echo $data;
?>
Also the html code hasn't the right format.
<p><a href="http://www.site.com</p>" title="">http://www.site.com</p></a>
should look:
<p><a href="http://www.site.com" title="">http://www.site.com</a></p>
<p><a href="http://www.site.com</p>" title="">http://www.site.com</p></a>
should look:
<p><a href="http://www.site.com" title="">http://www.site.com</a></p>
administradores: he needs to do the opposite.
ellandrd: this code will work with <p></p> in original string, and without:
<?php
function fcnConvertWebsiteAddress($ data)
{
$data = preg_replace('@(https?://( [-\w\.]+)+ (:\d+)?(/( [\w/_\.]*( \?\S+)?)?) ?)@', '<a href="$1" title="">$1</a>', $data);
return $data;
}
$data = '<p>http://www.site.com</p>'."\n";
$data .= '<p>http://site.com/contact.html</p>'."\n";
$data = fcnConvertWebsiteAddress($ data);
echo $data;
?>
ellandrd: this code will work with <p></p> in original string, and without:
<?php
function fcnConvertWebsiteAddress($
{
$data = preg_replace('@(https?://(
return $data;
}
$data = '<p>http://www.site.com</p>'."\n";
$data .= '<p>http://site.com/contact.html</p>'."\n";
$data = fcnConvertWebsiteAddress($
echo $data;
?>
Why not simply like that:
<?php
function fcnConvertWebsiteAddress($ data)
{
$data = strip_tags($data);
$data1 = preg_replace('/(https?\:\/ \/\S*)/',' <p><a href="$1" title="">$1</a><p>', $data);
return $data1;
}
$data = '<p>http://www.site.com</p>'."\n";
$data .= '<p>http://site.com/contact.html</p>'."\n";
$data = fcnConvertWebsiteAddress($ data);
echo $data;
?>
<?php
function fcnConvertWebsiteAddress($
{
$data = strip_tags($data);
$data1 = preg_replace('/(https?\:\/
return $data1;
}
$data = '<p>http://www.site.com</p>'."\n";
$data .= '<p>http://site.com/contact.html</p>'."\n";
$data = fcnConvertWebsiteAddress($
echo $data;
?>
excuse GEM100, obviously crossposted at same time as you did.
Definitely your solution is more elegant ; )
Definitely your solution is more elegant ; )
ASKER
GEM100
you solution doesnt work for formats such as www.site.com. it will only work if the website address has http:// in it...
you solution doesnt work for formats such as www.site.com. it will only work if the website address has http:// in it...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$data = '<p><a href="http://www.site.com</p>" title="">http://www.site.com</p></a>';
echo strip_tags($data);
?>