texpert
texpert

Reputation: 162

how to make php escape brackets in heredoc?

I want to escape brackets in heredoc (in php), for example,

$str = <<<EOD    
hello <hello inside>
EOD;

but when i echo this string, i just get "hello" as output

Upvotes: 2

Views: 1295

Answers (2)

Dereleased
Dereleased

Reputation: 10087

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);

Upvotes: 2

Pekka
Pekka

Reputation: 449475

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 &lt;hello inside&gt;
EOD;

Upvotes: 6

Related Questions