how to make php escape brackets in heredoc?
I want to escape brackets in heredoc (in php), for example,
$s开发者_如何学编程tr = <<<EOD
hello <hello inside>
EOD;
but when i echo this string, i just get "hello" as output
This has nothing to do with PHP really. It's your browser interpreting <hello inside>
as a tag.
I'm afraid there is no automatic way of turning this into HTML elements inside heredoc; you'd have to do a htmlspecialchars();
on the whole string, or use HTML entities:
$str = <<<EOD
hello <hello inside>
EOD;
I would have just left this as a comment to Pekka's answer, but you can't format comments this way. You can always treat HEREDOC/NOWDOC blocks just like strings (as long as nothing follows the closing identifier on that line), so this is perfectly valid:
$str = htmlentities(<<< EOD
hello <hello inside>
EOD
);
and is the same as:
$str = <<< EOD
hello <hello inside>
EOD;
$str = htmlentities($str);
精彩评论