首页 > 解决方案 > 如何替换字符串中的 \n?

问题描述

$data = $this->em->getRepository('App\\Entity\\Data')->findAll();

foreach ($data as $key => $value) {

      $str1 =      $value->getContent();
      $str2     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
      $order   = array("\r\n", "\n", "\r");
      $replace = '<br>';
      $newstr1 = str_replace($order, $replace, $str1);
      $newstr2 = str_replace($order, $replace, $str2);

}

$newstr2 的输出:

"Line 1<br>Line 2<br>Line 3<br>Line 4<br>"

$newstr1 的输出:

"Animals \n- 123 / N23\n- Monkeys together\n- 4 x 400ft. Farm rabits\n- 2 x 1000ft. Horses Cats\n- Group with all farm members\n- Flowers \n- Food\n- Love 400 -Random\r\n- 2-Countries\r\n- 1 x Hotel 4\"x4\"" \r\n- 1 x Shelter \r\n- Plate \r\n- Pillow \r\n- Cards"

我不明白,为什么\n没有在 $newstr2 中替换

标签: phpstr-replace

解决方案


推荐阅读