我正在编写一个网页,其中涉及使用字符串的 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 标记,但它并没有删除换行符。有什么办法可以摆脱这个换行符?