2008-12-17 79 views
2

我想格式化一些錯誤的html輸出到彈出窗口。 html存儲在mysql數據庫的一個字段中。htmlspecialchars和json編碼問題

我已經在該行中的PHP像這樣進行json_encode並用htmlspecialchars:

$html = htmlentities(json_encode($row2['ARTICLE_DESC'])); 

,並打電話給我makewindows功能,它只是簡單地採用HTML作爲一個放慢參數,並使用它withdocument.write像這樣:

<p><a href='#' onclick=\"makewindows('".$html."'); return false;\">Click for full description </a></p> 

此工程確定,如在一些HTML代碼產生時,如以下:

http://www.nomorepasting.com/getpaste.php?pasteid=22823&seen=true&wrap=on&langoverride=html4strict

粘貼那裏,因爲我不知道如何來換行,在SO

的問題是,用htmlspecialchars似乎並沒有被剝離不良的HTML數據,則不會創建任何彈出窗口。我收到螢火蟲的誤差後的參數列表

缺失)然而,HTML是我無法控制的。

從我讀過的內容,我正在採取正確的步驟。如果我錯過了一些東西,它是什麼?

我全使Windows功能:

function makewindows(html){ 
child1 = window.open ("about:blank"); 
child1.document.write(html); 
child1.document.close(); 
} 
+0

當我編寫PHP並且必須編寫我自己的自定義分析器來解析(或剝離)特殊字符時,`htmlentities`和`htmlspecialcharacters`非常令人沮喪。你可以設置你的數據庫表,只允許某個字符集或在插入前驗證數據嗎? – rxgx 2011-01-04 10:09:59

回答

1

你不應該在函數調用的單引號。它應該是這樣的:

<p><a href='#' onclick=\"makewindows(" . $html . "); return false;\">Click for full description </a></p> 

那麼輸出將看起來像

<p><a href='#' onclick="makewindows(&quot;.....&quot;); return false;">Click for full description </a></p> 

這是正確的。

0

試試以下的方法:

$html = htmlentities(json_encode($row2['ARTICLE_DESC']),ENT_QUOTES); 

我覺得單引號默認情況下不逃脫。儘管如此,我仍然建議您在打開窗口之前將html保存在JavaScript變量中。