我可以從數據庫中將數據讀取到我的角度應用程序,但我只是在文本中得到靜態\n而不是新行。我知道我應該將所有\n出現的情況轉換為<br />但我未能做到這一點,即使使用echo nl2br;//extract from my php fileif($result = mysqli_query($con, $sql)) { $animals = []; $cr = 0; while($row = mysqli_fetch_assoc($result)) { $animals[$cr]['animal'] = $row['animal']; $animals[$cr]['t1'] = $row['title1']; $animals[$cr]['p1'] = $row['paragraph1']; $cr++;}echo nl2br($animals); echo json_encode($animals);下面是我的角度文件//extract from my animals.component.html file <div class="container" *ngFor="let animal of this.animals"> {{animal.t1}} {{animal.p1}} {{animal.t2}} </div>但是,我在網頁上的輸出(來自animal.t1)與數據庫條目的文本完全相同:Animals \n are \n fun \n .我嘗試了許多不同的方法,但似乎無法將\n's 轉換為<br>. 有人對此有什么建議嗎?
2 回答

侃侃爾雅
TA貢獻1801條經驗 獲得超16個贊
nl2br接受一個字符串而不是一個數組。如果您在查詢中選擇列,那就容易得多。只需映射行數組:
? // SELECT animal, t1, p1 FROM table WHERE .....
? while($row = mysqli_fetch_assoc($result))
? {
? ? ? $animals[] = array_map('nl2br', $row);
? }
? echo json_encode($animals);
- 2 回答
- 0 關注
- 143 瀏覽
添加回答
舉報
0/150
提交
取消