我需要在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
匹配垂直空格.
// 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)
您的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
匹配垂直空格.
只需将文本行array_filter
分解为数组,删除空行并再次内爆数组.
$tmp = explode("\n", $str); $tmp = array_filter($tmp); $str = implode("\n", $tmp);
或者在一行中:
$str = implode("\n", array_filter(explode("\n", $str)));
我不知道,但这可能比快preg_replace
.
在从Bythos评论上述从杰米的链接为我工作:
/^\n+|^[\t\s]*\n+/m
我不想剥离所有新行,只是空/空白行.这就是诀窍!