donglaoping9702 2017-04-22 18:58
浏览 115
已采纳

PHP preg_replace替换换行没什么用

I have a text field retrieved by a Solr query that contains the body of an email. I am trying to replace embedded line breaks with paragraph tags via PHP like so:

$text = $item->Body[0];
$new_line = array("
", "
", "");
preg_replace($new_line,'</p><p>',$text);
echo $text;

When I show the result in my IDE/debugger the newline characters are not replaced and are still there: output example

I have been going through threads on this site trying patterns suggested by different people including "/\s+/" and PHP_EOL and "/( || )/" and nothing works. What am I doing wrong?

  • 写回答

3条回答 默认 最新

  • douxu4610 2017-04-22 19:01
    关注

    You are missing the delimiter around your regex strings and you are not assigning the value.
    You can also reduce your regex:

    $text = preg_replace("/?
    |/", '</p><p>', $text);
    

    You might want switch to the multibyte safe version. They work with Unicode and you don't need delimiters there ;)

    $text = mb_ereg_replace("?
    |", '</p><p>', $text);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作