当前位置:  开发笔记 > 编程语言 > 正文

如何从PHP中删除文本中的空白行?

如何解决《如何从PHP中删除文本中的空白行?》经验,为你挑选了4个好方法。

我需要在PHP中删除空白行(用空格或绝对空白).我使用这个正则表达式,但它不起作用:

$str = ereg_replace('^[ \t]*$\r?\n', '', $str);
$str = preg_replace('^[ \t]*$\r?\n', '', $str);

我想要的结果:

blahblah

blahblah

   adsa 


sad asdasd

将:

blahblah
blahblah
   adsa 
sad asdasd

Michael Wale.. 78

// New line is required to split non-blank lines
preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string);

上面的正则表达式说:

/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/
    1st Capturing group (^[\r\n]*|[\r\n]+)
        1st Alternative: ^[\r\n]*
        ^ assert position at start of the string
            [\r\n]* match a single character present in the list below
                Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
                \r matches a carriage return (ASCII 13)
                \n matches a fine-feed (newline) character (ASCII 10)
        2nd Alternative: [\r\n]+
            [\r\n]+ match a single character present in the list below
            Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
            \r matches a carriage return (ASCII 13)
            \n matches a fine-feed (newline) character (ASCII 10)
    [\s\t]* match a single character present in the list below
        Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
        \s match any white space character [\r\n\t\f ]
        \tTab (ASCII 9)
    [\r\n]+ match a single character present in the list below
        Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
        \r matches a carriage return (ASCII 13)
        \n matches a fine-feed (newline) character (ASCII 10)

return preg_replace("/(^ [\ r \n]*| [\ r \n] +)[\ s\t]*[\ r \n] + /","\n",$ string); 这项工作 (6认同)

使用http://regex101.com/生成示例中包含的说明. (6认同)


Alan Moore.. 25

您的ereg-replace()解决方案是错误的,因为这些ereg/eregi方法已被弃用.你preg_replace()甚至不会编译,但如果添加分隔符并设置多行模式,它将正常工作:

$str = preg_replace('/^[ \t]*[\r\n]+/m', '', $str);

m修改允许^一个逻辑行的开头匹配,而不是整个字符串的仅仅是个开始.起始线锚是必要的,因为如果没有它,正则表达式将匹配每行末尾的换行符,而不仅仅是空行换行符.你不需要行结束锚($),因为你正在积极匹配换行符,但它没有受到伤害.

该接受的答案能够完成任务,但它的复杂得多,它需要的.正则表达式必须匹配字符串的开头(^[\r\n]*未设置多行模式)或至少一个换行符([\r\n]+),后跟至少一个换行符([\r\n]+).因此,在以一个或多个空行开头的字符串的特殊情况下,它们将被替换为一个空行.我很确定这不是理想的结果.

但它在大多数情况下所做的是用一个换行符替换两个或多个连续的换行符,以及位于它们之间的任何水平空格(空格或制表符).无论如何,那是意图.作者似乎希望\s只匹配空格字符(\x20),实际上它匹配任何空格字符.这是一个非常常见的错误.实际列表从一种正则表达式的风格到下一种不同,但至少你可以期望\s匹配任何[ \t\f\r\n]匹配.

实际上,在PHP中你有一个更好的选择:

$str = preg_replace('/^\h*\v+/m', '', $str);

\h匹配任何水平空格字符,并\v匹配垂直空格.



1> Michael Wale..:
// New line is required to split non-blank lines
preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string);

上面的正则表达式说:

/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/
    1st Capturing group (^[\r\n]*|[\r\n]+)
        1st Alternative: ^[\r\n]*
        ^ assert position at start of the string
            [\r\n]* match a single character present in the list below
                Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
                \r matches a carriage return (ASCII 13)
                \n matches a fine-feed (newline) character (ASCII 10)
        2nd Alternative: [\r\n]+
            [\r\n]+ match a single character present in the list below
            Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
            \r matches a carriage return (ASCII 13)
            \n matches a fine-feed (newline) character (ASCII 10)
    [\s\t]* match a single character present in the list below
        Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
        \s match any white space character [\r\n\t\f ]
        \tTab (ASCII 9)
    [\r\n]+ match a single character present in the list below
        Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
        \r matches a carriage return (ASCII 13)
        \n matches a fine-feed (newline) character (ASCII 10)


return preg_replace("/(^ [\ r \n]*| [\ r \n] +)[\ s\t]*[\ r \n] + /","\n",$ string); 这项工作
使用http://regex101.com/生成示例中包含的说明.

2> Alan Moore..:

您的ereg-replace()解决方案是错误的,因为这些ereg/eregi方法已被弃用.你preg_replace()甚至不会编译,但如果添加分隔符并设置多行模式,它将正常工作:

$str = preg_replace('/^[ \t]*[\r\n]+/m', '', $str);

m修改允许^一个逻辑行的开头匹配,而不是整个字符串的仅仅是个开始.起始线锚是必要的,因为如果没有它,正则表达式将匹配每行末尾的换行符,而不仅仅是空行换行符.你不需要行结束锚($),因为你正在积极匹配换行符,但它没有受到伤害.

该接受的答案能够完成任务,但它的复杂得多,它需要的.正则表达式必须匹配字符串的开头(^[\r\n]*未设置多行模式)或至少一个换行符([\r\n]+),后跟至少一个换行符([\r\n]+).因此,在以一个或多个空行开头的字符串的特殊情况下,它们将被替换为一个空行.我很确定这不是理想的结果.

但它在大多数情况下所做的是用一个换行符替换两个或多个连续的换行符,以及位于它们之间的任何水平空格(空格或制表符).无论如何,那是意图.作者似乎希望\s只匹配空格字符(\x20),实际上它匹配任何空格字符.这是一个非常常见的错误.实际列表从一种正则表达式的风格到下一种不同,但至少你可以期望\s匹配任何[ \t\f\r\n]匹配.

实际上,在PHP中你有一个更好的选择:

$str = preg_replace('/^\h*\v+/m', '', $str);

\h匹配任何水平空格字符,并\v匹配垂直空格.



3> Ben..:

只需将文本行array_filter分解为数组,删除空行并再次内爆数组.

$tmp = explode("\n", $str);
$tmp = array_filter($tmp);
$str = implode("\n", $tmp);

或者在一行中:

$str = implode("\n", array_filter(explode("\n", $str)));

我不知道,但这可能比快preg_replace.



4> Dan Power..:

在从Bythos评论上述从杰米的链接为我工作:

/^\n+|^[\t\s]*\n+/m

我不想剥离所有新行,只是空/空白行.这就是诀窍!

推荐阅读
coco2冰冰
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有