我知道^
and $
,但我想删除字符串的最后一个空行,而不是每个。
$s = 'Foo
Bar
Baz
';
应该返回为
$s = 'Foo
Bar
Baz;
使用正则表达式如何在 PHP 中完成?
你可以在这里试试:http: //codepad.viper-7.com/p3muA9
我知道^
and $
,但我想删除字符串的最后一个空行,而不是每个。
$s = 'Foo
Bar
Baz
';
应该返回为
$s = 'Foo
Bar
Baz;
使用正则表达式如何在 PHP 中完成?
你可以在这里试试:http: //codepad.viper-7.com/p3muA9
<?php
$s = 'Foo
Bar
Baz
';
$s_replaced = preg_replace('//', '', $s);
$s_replaced = rtrim($s_replaced);
$out = '<textarea cols=30 rows=10>'.$s_replaced.'</textarea>';
echo $out;
?>
使用rtrim()
.
利用:
$s_replaced = preg_replace("/".PHP_EOL."$/", '', $s);
尝试这个:
查找:
(?s)\s+$
用。。。来代替:
none
解释:
<!--
(?s)\s+$
Options: case insensitive; ^ and $ match at line breaks
Match the remainder of the regex with the options: dot matches newline (s) «(?s)»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
-->