2015-10-06 64 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文件的代碼添加到我的問題中。 –