我知道^
和$
,但我想删除字符串的最后一行空行,而不是每个。
$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) «$»
-->