-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