开发者

Regex matching (anything)(dash)(numbers) and then replace

I need a little help on a regex pattern.

I have some strings ending with a dash and a number to the end to the string.

Example:

asddasasdasddasadsdsa-1
asdasdasdasd-11
asasdasdasdasd-234

I wan开发者_JS百科t to remove the (dash)(number to the end of string) so I need to match them and replace them with "".

I want to remove the LAST dash that is followed by only numbers to the end of the string.

Examples:

asdasd-1-1 must be asdasd-1 (last dash and number removed)

asdasd-1-1a must not change (there is no "(dash)(numbers only to the end of string)" so nothing changes)

Thank you!


Try:

$str = preg_replace('/-\d+$/', '', $str);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜