【问题标题】:How to properly use jQuery to output html string from php?如何正确使用 jQuery 从 php 输出 html 字符串?
【发布时间】:2015-10-06 09:42:11
【问题描述】:

我的 php 将从数据库中获取一串 html。检索到的字符串具有 html 标记、特殊字符(例如双引号和单引号)以及换行符。因此,字符串不能被 jQuery 直接使用。我想问是否有任何好的解决方案来删除所有换行符并动态转义这些特殊字符?提前谢谢你。

编辑

文字是从另一个网站随机复制的。很抱歉,还没有代码,因为我不知道该怎么做。但我将使用 jQuery 将 html 附加到其中一个。

<p class="story-body__introduction">President Barack Obama stood in the White House briefing room and, once again, railed against those who object to increased firearm regulation.</p>
<p>"Right now, I can imagine the press releases being cranked out,"&nbsp;<a class="story-body__link-external" href="http://time.com/4058961/oregon-shooting-president-obama-transcript-speech/">he said</a>. "We need more guns, they'll argue. Fewer gun safety laws. Does anybody really believe that?"</p>
<p>Mr Obama cited polls that find "the majority of Americans understand we should be changing these laws".</p>
<p>A mid-July survey by the Pew Research Center<a class="story-body__link-external" href="http://www.people-press.org/2015/08/13/continued-bipartisan-support-for-expanded-background-checks-on-gun-sales/">seems to support</a>&nbsp;his claim. Almost 80% of respondents backed laws preventing the mentally ill from purchasing firearms, and 70% were in favour of a national gun-sale database.</p>
<h2 class="story-body__crosshead">So the public support it, why doesn't it happen?</h2>
<p>Those numbers don't really mean much, however. What does matter is the opinion of members of the US Congress - and that legislative body is overwhelmingly against further gun regulation.</p>
<p>This disposition of Congress is a reflection of the disproportionate power of less-populated states in the Senate, the conservative-leaning composition of the current House congressional map and a Republican primary process that makes officeholders more sensitive to vehemently pro-gun-rights voters within their party.</p>
<p>Congress doesn't have to represent the views of the majority of Americans, at least as expressed in opinion surveys. It represents the views of Americans who go at the polls on Election Day and the simple majorities in the voting districts in which they cast their ballots.</p>

编辑

去除换行符和转义特殊字符的PHP函数:

public function getSingleLineHtml($html_input) {
    $output = str_replace(array("\r\n", "\r"), "\n", $html_input);
    $lines = explode("\n", $output);
    $new_lines = array();

    foreach($lines as $i => $line) {
        if(!empty($line)) {
            $new_lines[] = trim($line);
        }
    }
    return htmlspecialchars_decode(implode($new_lines), ENT_QUOTES);
}

输出html内容的phtml文件:

<div class="content-block" id="content-block-1"></div>

<?php
    $html_content = dataFromDb['content'];
    $html_content = getSingleLineHtml($html_content)
?>

<script>
    jQuery("#content-block-1").append('<?php echo $html_content; ?>');
</script>

【问题讨论】:

  • 将得到的字符串放入问题中。
  • htmlspecialchars (php.net/manual/en/function.htmlspecialchars.php) 有帮助吗?
  • 您希望 jQuery 对代码做什么?试试看 jQuery.parseHTML() 是否对你有帮助:api.jquery.com/jquery.parsehtml
  • @Danilo 我尝试了 jQuery.parseHTML(),但显示错误消息“参数列表后未捕获语法错误:缺少)”。我认为是由于字符串中的双引号和单引号造成的。
  • @AhesanaliMomin 函数 htmlspecialchars 也会转义 html 标签。因此,html 标签显示为纯文本。但是,应该保留html标签。

标签: javascript php jquery html


【解决方案1】:

您应该使用 htmlspecialchars_decode() 而不是 htmlspecialchars() php 函数。

【讨论】:

  • 它不起作用。我使用 htmlspecialchars_decode 来获取字符串。然后,我使用以下方式将字符串放入 javascript: jQuery('#element-1').append('');并且它仍然返回 "Uncaught SyntaxError: missing ) after argument list" 。
  • 我认为您应该粘贴您的实际完整代码,以便有人可以理解 clearlu
  • 感谢您的帮助。我只是将我的 phtml 文件的代码添加到我的问题中。
  • 我看到您的 html 还包含混合字符串,例如 ' 和 "。
  • 如果你在其他地方使用'在开始时使用'。如果你必须使用'而不是你必须使用转义序列
猜你喜欢
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 2011-01-20
  • 2012-05-13
  • 2012-09-30
  • 1970-01-01
  • 2021-01-14
相关资源
最近更新 更多