string - 使用 PHP 删除字符串中的最后一个数字?

标签 string numbers

我想从此字符串中删除最后一个数字(包括破折号):

hello-world-093

结果应该是:

hello-world

最佳答案

http://php.net/manual/en/function.preg-replace.php

混合 preg_replace (混合 $pattern , 混合 $replacement , 混合 $subject [, int $limit = -1 [, int &$count ]] );

在你的情况下它应该是这样的:

$subject = 'hello-world-093' ;
$subject_stripped = preg_replace ( '/-[0-9]*$/' , '' , $subject);

上述模式将删除 - 以及字符串末尾后面的所有数字。

关于string - 使用 PHP 删除字符串中的最后一个数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15213452/

相关文章:

python - pandas - 按部分字符串分组

c++ - 解析 URL 字符串以删除不需要的内容 (C++)

c++ - 不让一个值超过另一个值的最快方法 - C++

swift - TextField 未更新,给我可选的 ""

javascript - 如何合并排序的数字列表中的连续数字?

python - 根据模板编号将数字格式化为字符串

python - 字符串的列表理解

c - 在c中生成随机字符串

java - 确定字符串中是否存在转义字符

c# - 文本框中的数字值添加逗号