使用带引号的隐藏输入值时出现问题


trouble using hidden input value with quotes

我搜索了网站,没有找到解决方案。我的问题是,我有一个隐藏的输入,我想通过post方法发送,其中有引号。我试过使用addslashes(),我得到同样的问题。它现在看起来像这样:

<?php $value = 'I''ve got '"some'" random text with quotes'; ?>
<input name="example" value="<?=$value?>">

我得到的大部分文本显示在我的形式,因为引号没有被忽略啊!,)那么我如何得到文本引用到一个隐藏的输入?

提前感谢!

<?php $value = "I've got '"some'" random text with quotes"; ?>

当你输出这将导致以下结果?

<input name="example" value="I've got '"some'" random text with quotes">

我会转换它们,这样它们就可以验证并避免混淆:

<?php $value = 'I&#039;ve got &quot;some&quot; random text with quotes'; ?>
<input name="example" value="<?=$value?>">

尽量避免在PHP字符串中使用双引号,因为PHP将在整个字符串中搜索要解析的变量,而不管字符串是否包含一个变量。它们比单引号慢。现在已经没有这么多了,但是在字符串中使用单引号仍然是一个很好的做法。