I want to give a text to a DIV. This text has HTML tags, so I thought its enough to escape the " and ' characters. Full code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
".dtd">
<html xmlns="" xml:lang="en">
<head>
</head>
<body>
<div id="lay">.</div>
<?php
$a = 'Missing<hr />argument 3 for Class::method(), called in X:\directory\dir2\dir3\x';
$a = str_replace(array("\"", "'"), array(""", '''), $a);
?>
<script type="text/javascript">
document.getElementById('lay').innerHTML = '<?php echo $a; ?>';
</script>
</body>
</html>
but firefox said its malformed. Even htmlspecialchars() aint work. How the heck to escape this string? And why it fails?
I want to give a text to a DIV. This text has HTML tags, so I thought its enough to escape the " and ' characters. Full code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml" xml:lang="en">
<head>
</head>
<body>
<div id="lay">.</div>
<?php
$a = 'Missing<hr />argument 3 for Class::method(), called in X:\directory\dir2\dir3\x';
$a = str_replace(array("\"", "'"), array(""", '''), $a);
?>
<script type="text/javascript">
document.getElementById('lay').innerHTML = '<?php echo $a; ?>';
</script>
</body>
</html>
but firefox said its malformed. Even htmlspecialchars() aint work. How the heck to escape this string? And why it fails?
Share Improve this question asked Oct 19, 2011 at 6:47 user893856user893856 1,0293 gold badges15 silver badges21 bronze badges3 Answers
Reset to default 3Just use addslashes
like so:
$a = 'Missing<hr />argument 3 for Class::method(), called in X:\directory\dir2\dir3\x';
$a = addslashes($a);
$a = str_replace(array("\"", "'"), array(""", '''), $a);
As the documentation says:
Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash () and NUL (the NULL byte).
the "\x" at the end of you string is now trying to expect a hexadecimal digit you need to escape the slash in front of the x.
try adding three slashes
$a = 'Missing<hr />argument 3 for Class::method(), called in X:\directory\dir2\dir3\\\x';
I got the solution for this. I was displaying image using physical path in browser: "D:/wamp/www/project/images/imag.png"
-> malformed Unicode character escape sequence
I set it to URL then it resolved my problem "http://siteurl/images/imag.png"