-1
我寫的是涉及使用字符串的PHP數組輸出JS數組如下我怎樣才能確保一個PHP到JS字符串不會打破
while($main2row = mysqli_fetch_assoc($result)) {
$tSummary[$main2row['id']] = '"'.$main2row['content'].'"';
$tTitle[$main2row['id']] = '"'.$main2row['title'].'"';
}
//the above gets the php from the sql database, then stores it in an array, both are string arrays
foreach ($tSummary as $key => $val)
{
echo "summArray['".$key."'] = ".nl2br($val).";\n";
}
foreach ($tTitle as $key => $val)
{
echo "titleArray['".$key."'] = ".nl2br($val).";\n";
}
//these foreach loops will print in a js script and will store the php arrays as js arrays
我知道,我也許應該是網頁使用JSON,但這是它爲別人設置的方式,我覺得我應該堅持自己的代碼。
的問題是,在存儲數據時,按以下方式失敗:
summArray['225594172793552897'] = " blah blah blah blah blah - blah.<br />
<br />
blah blah blah blah....."; /* this one is messed up */
summArray['225595300046307328'] = "text text text text text text text text text text text text text text ."; /* this one is fine */
我注意到,該字符串休息的地方有一個換行符,所以我試圖用nl2br但是,而添加了一個br標籤,它並沒有刪除換行符。有沒有什麼辦法可以擺脫這種折線?
'json_encode()'字符串。如果它們很複雜,它們將被構建成JavaScript對象或數組類型,並且如果普通字符串將被正確轉義。 – 2012-07-18 17:24:29