使用 PHP 从字符串中删除最后一个数字?

Jos*_*lan 4 string numbers

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

hello-world-093

结果应该是:

你好,世界

Meh*_*sly 5

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

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

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

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

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