【问题标题】:Laravel <br> to newlineLaravel <br> 换行
【发布时间】:2017-01-26 20:47:51
【问题描述】:

我知道当想将&lt;br&gt; 标记转换为新行时,我可以做一个简单的替换。但是我遇到了解析问题,因为提供的&lt;br&gt; 标签不为空。

<br style=\"color: rgb(83, 83, 83); font-family: \" helvetica=\"\" ...

后端不是我的,所以在这里讨论编码的好坏没有意义,我只是想知道是否有解决方案可以用简单的新行替换那些。

类似于nl2br() 但相反。

编辑:

当我知道“为什么”是我尝试过的东西不起作用时,不知道显示代码有什么用......但是这里有

public function removeSingleHtmlFormatting($single)
{
    $single->short_description = str_replace("<br>", "\r\n", $single->short_description);
    $single->short_description = strip_tags($single->description);
    $single->short_description = preg_replace("/&nbsp;/", " ", $single->short_description);
}

当然替换不起作用,因为没有这样的字符串可以替换...我不知道从哪里开始解析它

【问题讨论】:

    标签: php html json laravel parsing


    【解决方案1】:

    而不是

    str_replace("<br>", "\r\n", $single->short_description);
    

    试试

    preg_replace("/<br.*>/U", "\r\n", $single->short_description);
    

    这样正则表达式匹配&lt;br&gt;,包括其中的任何内容,而不仅仅是空的&lt;br&gt;

    【讨论】:

      猜你喜欢
      • 2021-12-18
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      • 2022-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多