2015-10-06 65 views
0

我的PHP将从数据库中获取一串html。检索到的字符串包含html标记,特殊字符(如双引号和单引号)和换行符。因此,该字符串不能被jQuery直接使用。我想问是否有任何好的解决方案来删除所有换行符并动态地转义那些特殊字符?先谢谢你。如何正确使用jQuery从PHP输出html字符串?

编辑

文本是随机从其他网站复制。我很抱歉没有任何代码,因为我不知道如何去做。但我将使用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); 
} 

PHTML文件,该文件输出HTML内容:

<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> 
+0

把你得到的字符串放在问题中。 – Abhi

+0

htmlspecialchars(http://php.net/manual/en/function.htmlspecialchars.php)有帮助吗? –

+0

您希望jQuery如何处理代码?尝试看看如果jQuery.parseHTML()可能会帮助你:http://api.jquery.com/jquery.parsehtml/ – Danilo

回答

0

你应该有使用htmlspecialchars_decode()而不是ht mlspecialchars()php函数。

+0

它不起作用。我使用htmlspecialchars_decode来获取一个字符串。然后,我使用以下方式将字符串转换为javascript:jQuery('#element-1')。append('<?php $ content;?>');它仍然返回“Uncaught SyntaxError:missing”参数列表后“。 –

+0

我认为你应该粘贴你的实际完整代码,这样才能理解clearlu –

+0

谢谢你的帮助。我只是将我的phtml文件的代码添加到我的问题中。 –