I'm trying to use preg_replace to replace
<a href="WWW.ANYURL.COM">DISPLAY_TEXT</a>
with
<a href="WWW.ANYURL.COM">DISPLAY_TEXT</a>
here is my code:
$string = htmlentities(mysql_real_escape_string($string1));
$newString = preg_replace('#<a\ href="([^&]*)">([^&]*)</a>#','<a href="$1">$2</a>',$string);
If I do limited tests such as:
$newString = preg_replace('#<a\ href#','TEST',$string);
then
<a href="WWW.ANYURL.COM">DISPLAYTEXT</a>
becomes
TEST="WWW.ANYURL.COM">DISPLAYTEXT</a>
But if I try to get it to also match the "=" it acts as if it could't find a match, i.e.
$newString = preg_replace('#<a\ href=#','TEST',$string);
returns the original unchanged:
<a href="WWW.ANYURL.COM">DISPLAY_TEXT</a>
I've been going at this for a couple hours, any help would be greatly appreciated.
EDIT: code in context
$title = clean_input($_POST['title']);
$story = clean_input($_POST['story']);
function clean_input($string)
{
if(get_magic_quotes_gpc())
{
$string = stripslashes($string);
}
$string = htmlentities(mysql_real_escape_string($string));
$findValues = array("<b>","</b>");
$newValues = array("<b>", "</b>");
$newString = str_replace($findValues, $newValues, $string);
$newString2 = preg_replace('#<a\ href="([^&]*)">([^&]*)</a>#','<a href="$1">$2</a>',$newString);
return $newString2;
}
Sample $story = Lorem ipsum dolor sit amet, consectetur adipiscing elit. <a href="www.google.com">Google</a>
Vivamus quis sem felis. Morbi vitae neque ac neque blandit malesuada lobortis sit amet justo. Donec convallis, nibh ut lacinia tempor, neque felis scelerisque nibh, at feugiat lectus erat in nulla. In et euismod nunc. <pernicious code></code>
Pellentesque vitae ante orci, vitae ultrices neque. <a href="www.yahoo.com">Yahoo</a>
In non nulla sapien, vestibulum faucibus metus. Fusce egestas viverra arcu, <b>ac</b>
sagittis leo facilisis in. Nulla facilisi.
I want only a few tags like href and bold to be allowed through as code.
htmlentities
if you're going to just undo the work ofhtmlentities
? - thetaikohtml_entity_decode()
and just get on with more important things... - Marc Bstrip_tags($str, '<a>')
gives you a proper result instead ofhtmlentities()
- sod