我知道当想要转换时我可以做一个简单的替换 <br>
标记到新行。但我面临解析问题,因为提供了 <br>
标签不为空。
<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("/ /", " ", $single->short_description);
}
当然替换不起作用,因为没有这样的字符串可以替换...我不知道从哪里开始解析它
最佳答案
而不是
str_replace("<br>", "\r\n", $single->short_description);
尝试
preg_replace("/<br.*>/U", "\r\n", $single->short_description);
这样正则表达式就匹配 <br>
包括里面的任何东西,不仅仅是空的<br>
.
关于php - Laravel <br> 到换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39568483/