-1

次のように、文字列のphp配列を使用してjs配列を出力するWebページを作成しています

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タグが追加されましたが、改行は削除されませんでした。この改行を取り除く方法はありますか?

4

1 に答える 1

0

の代わりに、 JavaScript で改行を取得するか、HTML の改行も取得してnl2br($val)みてください。str_replace("\n", "\\n", $val)str_replace("\n", "\\n", nl2br($val))

于 2012-07-19T09:45:31.100 に答える