代码之家  ›  专栏  ›  技术社区  ›  Sumithran

如何使用preg_replace()仅替换特定行

  •  0
  • Sumithran  · 技术社区  · 7 年前

    我有一个数据表的顺序是这样的

    123.- sumi hai
    bla
    124.- the
    secod line
    is blah
    125.- another line
    

    我想用php preg_replace()得到这样的结果

    123.- sumi hai blah
    124.- the second line is blah
    125.- another line
    

    请帮帮我 提前谢谢

    1 回复  |  直到 7 年前
        1
  •  2
  •   Federico Piazza    7 年前

    您可以使用这样的正则表达式:

    \n^(?=[a-z])
    

    Working demo

    $str = '123.- sumi hai
    bla
    124.- the
    secod line
    is blah
    125.- another line';
    
    $result = preg_replace('/\n^(?=[a-z])/m', ' ', $str);
    
    echo "The result of the substitution is ".$result;