【问题标题】:How to convert quotes(',") from special char to general with other tags remaining special?如何在其他标签保持特殊的情况下将引号(',")从特殊字符转换为一般字符?
【发布时间】:2015-10-31 03:59:26
【问题描述】:

在我的字符串中,我有引号(',")< tags >。我用过

htmlspecialchars($str, ENT_QUOTES);  

仅将单引号和双引号转换为一般文本。但不幸的是,它也在转换标签 < , > 。出于这个原因,即使我有

strip_tags("$desc","< b >< font >< i >< u >< br >"); 

那些允许的标签也显示为一般的&lt; and &gt; 符号不能作为 html 标签工作。

最后,我想将单引号和双引号显示为常规文本,并允许标签像 html 一样工作。

谢谢。

【问题讨论】:

  • 如果它们在字符串中 try:str_replace("'","",$desc) 它将删除所有'
  • 我不想删除它。我希望它们作为一般文本而不是特殊字符。
  • 那你为什么不把'替换成\'

标签: php html tags


【解决方案1】:

为什么不在 htmlspecialchars() 之前运行 strip_tags() ?

样本:

<?php
$input = '<b>allowed tag</b> " <font>not allowed tag</font>';

// XXX
$tmp = htmlspecialchars($input, ENT_QUOTES);
$result = strip_tags($tmp, '<b>');
var_dump($result);
// string(78) "&lt;b&gt;allowed tag&lt;/b&gt; &quot; &lt;font&gt;not allowed tag&lt;/font&gt;"

// Improved
$tmp = strip_tags($input, '<b>');
$result = htmlspecialchars($tmp, ENT_QUOTES);
var_dump($result);
// string(53) "&lt;b&gt;allowed tag&lt;/b&gt; &quot; not allowed tag"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 2016-07-08
    • 2013-03-08
    • 1970-01-01
    相关资源
    最近更新 更多