摆脱不必要的双引号

时间:2012-10-08 14:57:11

标签: php json

echo '<tr class="questiontd"><td>'.htmlspecialchars($question).'</td>';
echo "<td class='addtd'><button type='button' class='add' onclick=\"parent.addwindow('$question');\">Add</button></td></tr>";

现在上面的代码将显示例如:

  

问题按钮

     

“什么是2 + 2”添加(按钮)

但我不希望""围绕这个问题,我希望它显示如下:

  

问题按钮

     

什么是2 + 2添加(按钮)

有谁知道如何删除文本周围的双引号? (如果您需要更多代码,请向我发表评论)

4 个答案:

答案 0 :(得分:4)

您正在将文字插入HTML。要逃避它,请使用htmlspecialchars。 HTML不是JSON。请勿使用json_encode

echo '<tr class="questiontd"><td>'.htmlspecialchars($question).'</td>';

答案 1 :(得分:0)

从第二行删除json_encode。如果你对字符串进行json编码,它将始终是双引号。

答案 2 :(得分:0)

只需使用htmlspecialchars()即可删除它们。

echo '<tr class="questiontd"><td>', htmlspecialchars($question), '</td>';

执行的翻译是:

'&' (ampersand) becomes '&amp;'
'"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set.
"'" (single quote) becomes '&#039;' (or &apos;) only when ENT_QUOTES is set.
'<' (less than) becomes '&lt;'
'>' (greater than) becomes '&gt;'

答案 3 :(得分:0)

这是真的

  

当你只是吐出html时,为什么json编码问题?不要编码,你可能不会得到报价。

但是对于一个简单的解决方案,如果你仍然坚持使用json_encode

echo '<tr class="questiontd"><td>'.str_replace(json_encode($question),"\"","").'</td>';

或更优雅

echo '<tr class="questiontd"><td>'.htmlspecialchars($question).'</td>';
相关问题