正则表达式 - 删除末尾的空行

regex - remove empty lines at the end

我想删除邮件末尾的空引号行:

> Hello
> 
> thats quotet>
>
>

> Hello
> 
> thats quotet>

但是正则表达式 preg_replace("/^>\W*$/m", "", $input_lines) 不起作用,因为它还删除了不在末尾的空行的 >。

编辑:问题仍然存在,只有行首的 > 应该被删除

您可以使用:

$result = preg_replace('~^(?:^>\h*\R?)*+\z~m', '', $input_lines);

为避免无用的测试,您还可以尝试这种更手动的模式:

$result = preg_replace('~^(?:^>\h*\R?)*+(*SKIP)\z~m', '', $input_lines);

使用rtrim,它从字符串的右侧删除指定的字符。我们要删除末尾的“>”和换行符。

代码:

echo rtrim($input, ">".PHP_EOL);

结果:

> Hello
> 
> thats quotet