如何让heredoc中的php转义括号?
问题描述:
我想逃避定界符括号中(PHP), 例如,如何让heredoc中的php转义括号?
$str = <<<EOD
hello <hello inside>
EOD;
,但是当我赞同这个字符串,我只是得到“你好”作为输出
答
这有什么好做PHP真的。这是您的浏览器将<hello inside>
解读为标签。
恐怕没有自动的方法把它变成heredoc里面的HTML元素;你必须对整个字符串做htmlspecialchars();
,或使用HTML实体:
$str = <<<EOD
hello <hello inside>
EOD;
答
我刚刚离开这个为佩卡的回答评论,但你不能格式化意见这种方式。您可以随时把HEREDOC/NOWDOC块就像串(只要没有遵循该行结束标识符),所以这是完全合法的:
$str = htmlentities(<<< EOD
hello <hello inside>
EOD
);
并且是一样的:
$str = <<< EOD
hello <hello inside>
EOD;
$str = htmlentities($str);
你能否澄清你的问题? :) – fresskoma 2010-11-02 21:41:43