如何在使用单引号的回声中使用单引号

Nas*_*Zia 15 php quotes echo

首先,我已经完成了相关的问题..没有找到任何答案..我使用此代码来显示消息

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

我怎样才能做到这一点,因为这个回声中的任何引用都会破坏声明......

Mad*_*iha 27

使用反斜杠转义引号,或使用双引号指定字符串.

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

echo "Here goes your message with an apostrophe S like thi's";
Run Code Online (Sandbox Code Playgroud)


He *_*Hui 5

使用反斜杠转义报价。

'hello\'s'
Run Code Online (Sandbox Code Playgroud)

反斜杠后出现的单引号将出现在屏幕上。


joh*_*jik 5

您尝试过addslashes()函数吗?它甚至使用你的例子。

我个人更喜欢函数htmlspecialchars() ,它执行相同的操作,但具有允许您指定其行为的标志。

像这样:

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

这会在 HTML 网页上正确显示该字符串。