Jump to content

Replacing quotes outside of html tags with html_entities


masteroleary

Recommended Posts

I need to convert:

"This is Amelia's Description!" <span style="font-weight: bold;">Hello, World!</span>

 

into:

"This is Amelia\'s Description!" <span style="font-weight: bold;">Hello, World!</span>

 

only modifying single and double quotes outsite of the html tag carats

 

This is what I have so far:

function filter_Desc($string)
{
$string = strip_tags($string, '<b><strong><i><em><br><br /><p><span>');
$string = eregi_replace('<', '[', $string);
$string = eregi_replace('>', ']', $string);
$string = htmlspecialchars($string, ENT_NOQUOTES);
$string = trim($string);
$string = addslashes($string);

return $string;
}

 

But obviously the problem with this is that it affects the double quotes within the html tags.

Input from form element: "This is Amelia's Description!" <span style="font-weight: bold;">Hello, World!</span>

 

Page to test input:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<?php
function filter_Desc($string)
{
$string = strip_tags($string, '<b><strong><i><em><br><br /><p><span>');
$string = preg_replace_callback("/(?<=>)([^<]+)/",
create_function('$matches',	'return htmlspecialchars($matches[0], ENT_NOQUOTES)') ,
$string);
$string = eregi_replace('<', '[', $string);
$string = eregi_replace('>', ']', $string);
$string = trim($string);

return $string;
}
if($_POST['text'])
{
echo "What you submitted:  " . $_POST['text'];
echo "What was left:  " . filter_Desc($_POST['text']);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="text" type="text" />
<input name="submit" type="submit" />
</form>
</body>
</html>

 

Error im getting:

What you submitted: \"This is Amelia\'s Description!\" Hello, World!
Parse error: parse error, unexpected '}' in C:\WORK FOLDER\Websites\Mabos3-New\test.php(15) : runtime-created function on line 1

Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, '', to be a valid callback in C:\WORK FOLDER\Websites\Mabos3-New\test.php on line 16
What was left: \"This is Amelia\'s Description!\" [span style=\"font-weight: bold;\"]Hello, World![/span]

 

Two problems. First I dont understand the error. I dont see a '}' on that line. Second, my html tags are being removed: \"This is Amelia\'s Description!\" Hello, World!

 

Maybe when I can figure out error one number two will go away.. maybe

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.