douzhiba6873 2013-08-27 13:56
浏览 36
已采纳

在所有段落之后添加新的额外行

I am fetching content from a url and storing them in database. Before this i am converting all content in plain text. for this i did this.

I wants to add new extra line after all paragraph.

I have tried but not so clear result..

$string = "<p>As a result, his move to Microsoft has raised many questions about the Redmond-based company's plans in the PC gaming space.</p><p>With the Xbox One launch looming, Microsoft has greatly de-emphasized PC gaming of late. </p><p>Holtman's hiring could signal a renewed emphasis on the computer, though.</p>

It seems like a guy who comes from Valve who has no peer, in my mind, in the gaming space relative to really strong B-to-C [business to consumer] relations could indicate a ramp up in the importance of that space, says John Taylor, managing director at Arcadia Investment Corp.

" ;
$search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript    
            '@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags    
            '@<style[^>]*?>.*?</style>@siU', // Strip style tags properly    
            '@<![\s\S]*?--[ \t
]*>@' // Strip multi-line comments including CDATA
        );

// remove excess whitespace        
// looks for a one or more spaces and replaces them all with a single space.        
$string = preg_replace($search, '', $string);        
$string = preg_replace('/ +/', ' ', $string);        
// check for instances of more than two line breaks in a row    
// and then change them to a total of two line breaks          
$string = preg_replace('/(?:(?:
||
)\s*){2}/s', "

", $string);
file_put_contents('testing.txt', $string );
  • 写回答

2条回答 默认 最新

  • doulu4413 2013-08-27 14:19
    关注

    You didn't give the wanted output, but I think you want something like :

    <p>Text</p>
    
    <p>Another text</p>
    
    

    Instead of using heavy REG EXP, just explode on </p> and add your extra lines :

    $array = explode ('</p>', $string);
    new_string = '';
    $temp = count ($array);
    foreach ($array as $key => $paragraph)
    {
        if ($key !== $temp - 1);
            $new_string .= $paragraph . "</p>
    ";
        else
            $new_string .= $paragraph;
    }
    

    The $new_string var should be what you're looking for, tell me if I'm right. It adds after every </p>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题