php 中格式错误的 unicode/十六进制
我想向 DIV 发送文本。这段文本有 HTML 标签,所以我认为它足以转义 " 和 ' 字符。完整代码:
<!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" 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>
但 Firefox 说它格式错误。甚至 htmlspecialchars() 也不起作用。到底如何转义这个字符串?为什么会失败?
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.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
字符串末尾的“\x”现在尝试期待一个十六进制数字,您需要转义 x 前面的斜杠。
尝试添加三个斜杠
$a = 'Missing
argument 3 for Class::method(), called in X:\directory\dir2\dir3\\\x';
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';
我得到了这个问题的解决方案。
我在浏览器中使用物理路径显示图像:
“D:/wamp/www/project/images/imag.png”
->格式错误的 Unicode 字符转义序列
我将其设置为 URL 然后它解决了我的问题
“http://siteurl/images/imag.png”
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"
只需像这样使用
addslashes
即可:正如文档所述 :
Just use
addslashes
like so:As the documentation says: