2010-11-02 38 views
2

我想逃避定界符括號中(PHP), 例如,如何讓heredoc中的php轉義括號?

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

,但是當我贊同這個字符串,我只是得到「你好」作爲輸出

+0

你能否澄清你的問題? :) – fresskoma 2010-11-02 21:41:43

回答

6

這有什麼好做PHP真的。這是您的瀏覽器將<hello inside>解讀爲標籤。

恐怕沒有自動的方法把它變成heredoc裏面的HTML元素;你必須對整個字符串做htmlspecialchars();,或使用HTML實體:

$str = <<<EOD  
hello &lt;hello inside&gt; 
EOD; 
2

我剛剛離開這個爲佩卡的回答評論,但你不能格式化意見這種方式。您可以隨時把HEREDOC/NOWDOC塊就像串(只要沒有遵循該行結束標識符),所以這是完全合法的:

$str = htmlentities(<<< EOD 
hello <hello inside> 
EOD 
); 

並且是一樣的:

$str = <<< EOD 
hello <hello inside> 
EOD; 
$str = htmlentities($str);