从
我拥有的字符串中删除数字的最简单方法是什么?字符串如下所示:
Green 1
Green 10
Green 100
Green 101
Green 102
Green 103
Green 104
Green 105
Green 106
Green 107
Green 108
Green 109
100 Bullets 1
100 Bullets 10
100 Bullets 11
100 Bullets 12
100 Bullets 13
100 Bullets 2
100 Bullets 3
我希望它们看起来像这样:
Green
Green
Green
Green
Green
Green
Green
Green
Green
Green
Green
Green
100 Bullets
100 Bullets
100 Bullets
100 Bullets
100 Bullets
100 Bullets
100 Bullets
我的主要重点是从字符串中删除数字。我认为正则表达式对此有好处。
您可以使用
的 rexeg 是 'd+$
。
正则表达式解释:
NODE EXPLANATION
--------------------------------------------------------------------------------
'd+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
$ before an optional 'n, and the end of the
string
工作代码
$rString = "12 foobar 123";
$nString = preg_replace("/'d+$/","",$rString);
echo $nString;
查看工作示例
注意
因为我们只看字符串的末尾,所以我们需要确保结尾不包含空格。您可以使用 PHP 中的 trim()
函数来解决此问题。
$rString = trim("12 foorbar 123 ");
我认为preg_replace("#'s+'d+$#", "", $item);
应该可以解决问题,但您必须逐行解析列表。
你可以试试'd+$
.确保先修剪字符串