Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法将mySQL数据库条目文本“\n”转换为“”格式?_Php_Html_Angular_Nl2br - Fatal编程技术网

Php 无法将mySQL数据库条目文本“\n”转换为“”格式?

Php 无法将mySQL数据库条目文本“\n”转换为“”格式?,php,html,angular,nl2br,Php,Html,Angular,Nl2br,我可以从数据库读取数据到angular应用程序,但我的文本中只给出了静态\n而不是新行。我知道我应该将所有\n事件转换为,但我没有这样做,即使使用echo nl2br 下面是我的角度文件 //extract from my animals.component.html file <div class="container" *ngFor="let animal of this.animals"> {{animal.t

我可以从数据库读取数据到angular应用程序,但我的文本中只给出了静态\n而不是新行。我知道我应该将所有\n事件转换为,但我没有这样做,即使使用echo nl2br

下面是我的角度文件

//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,与数据库条目的文本相同:

动物是有趣的

我尝试了许多不同的方法,但似乎无法将\n转换为。有人对此有什么建议吗?

使用nl2br:

nl2br采用字符串而不是数组。如果在查询中选择列,则会更容易。只需映射行数组:

  // SELECT animal, t1, p1 FROM table WHERE .....

  while($row = mysqli_fetch_assoc($result))
  {
      $animals[] = array_map('nl2br', $row);
  }
  echo json_encode($animals);

如果Angular正在将HTML转换为实体,则您可能希望查看此处

采用字符串而不是数组。@MrMan此外,从PHP手册:返回字符串,并在所有换行符之前插入\r\n\r\n和\r\n。所以说实话,实际上没有进行替换。谢谢你的回答!它似乎在改变它的工作,但打印出的网页上,我现在得到的是动物是有趣的。你知道为什么html没有呈现这些换行符吗?我不知道,但它可能正在转换html实体,所以虽然这段代码可能会解决OP的问题,但最好包含一个关于代码如何解决OP问题的解释。通过这种方式,未来的访问者可以从您的帖子中学习,并将其应用到自己的代码中。因此,它不是一种编码服务,而是一种知识资源。此外,高质量、完整的答案更有可能被提升。这些特性,以及所有帖子都是独立的要求,是SO作为一个平台的一些优势,使其区别于论坛。您可以编辑以添加其他信息和/或使用源文档补充说明。
$animals[$cr]['t1'] = nl2br($row['title1']);
  // SELECT animal, t1, p1 FROM table WHERE .....

  while($row = mysqli_fetch_assoc($result))
  {
      $animals[] = array_map('nl2br', $row);
  }
  echo json_encode($animals);