How to use single quote inside an echo which is using single quote

ぐ巨炮叔叔 提交于 2019-11-26 09:09:56

问题


First of all, i have gone through the related questions.. haven\'t found any answer.. I m using this code to display a message

echo \'Here goes your message with an apostrophe S like thi\'s \';

How can i make this work, as any quote inside this echo will break the statement...


回答1:


Either escape the quote with a backslash, or use double quotes to designate the string.

echo 'Here goes your message with an apostrophe S like thi\'s';

echo "Here goes your message with an apostrophe S like thi's";



回答2:


Escape the quote using a backslash.

'hello\'s'

The single quote that appears after the backslash will appear on screen.




回答3:


Have you tried the function addslashes()? It even uses your example.

I personally prefer the function htmlspecialchars() which does the same thing but has flags which let you specify its behavior.

like so:

echo htmlspecialchars("O'Rielly", ENT_QUOTES);

This shows the string properly on an HTML webpage.




回答4:


echo <<<EOT
You can put what ever you want here.. HTML, " ' ` anyting will go
Here goes your message with an apostrophe S like thi's
EOT;

Be sure to read this before using such kind of strings.




回答5:


In PHP, the escape sequence character is the backslash (\). You can add this before special characters to ensure that those characters are displayed as literals. For example:

echo 'Here goes your message with an apostrophe S like thi\'s ';

Or you can also write like this:

echo "Here goes your message with an apostrophe S like thi's ";



回答6:


Since the methods from the answers didn't work for my situation I ended up just calling a new echo everytime the type of quote changed through the code and swapped the type of quote to start the echo, it's 2019 now and I don't know about any other solution since I am really new to programming but this worked fine for me, example:

        else {
          echo '<a onclick="document.getElementById(';
          echo "'open_login').style.display='block'";
          echo '" class="branding w3-bar-item w3-button w3-mobile w3-light-blue w3-hover-white w3-right"href="#login"><span class="fa fa-user"></span>&nbsp;&nbsp;Login do Aluno</a>';
        }


来源:https://stackoverflow.com/questions/11792990/how-to-use-single-quote-inside-an-echo-which-is-using-single-quote

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