PHP: Work with a large string with quotes

百般思念 提交于 2020-01-21 19:43:10

问题


Suppose I have a large string of HTML code. It has both double quotes and single quotes. How can I work with such a string? I want to assign it to a php var but there's a quote clash whether I use double quotes or single quotes. Is there any way to assign this string to a var without having to manually add slashes to all the quotes?

It could be something like this:

<div class="name">Sally O'Connel</div>

Only the string is longer so there will be multiple occurances of single quotes and double quotes.


回答1:


You could use HEREDOC/NOWDOC:

$var = <<<HTML
<div class="name">Sally O'Connel</div>
HTML;



回答2:


 $var = file_get_contents('file.with.your.long.and.windy.html');

would be apparently the only sane way. PHP files are for the code.




回答3:


use Heredoc

$var = <<<HTML
<div class="name">Sally O'Connel</div>
HTML;

Please ensure that there is no space character after HTML;



来源:https://stackoverflow.com/questions/23885155/php-work-with-a-large-string-with-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!