繁体   English   中英

从字符串中获取最后一位数字

[英]Getting the last digits from string

我有这个字符串:tag:domain.com,2012-10-12:feed / channel / id / 335

我试图从字符串中获取最后一个数字到一个变量中。 该字符串中的日期也是动态的,但是我不需要在变量中使用该日期。

这是我的代码:

$string = "tag:domain.com,2012-10-12:feed/channel/id/335";

preg_match('/tag\:domain\.com,|\d+|-|\d+|-|\d+|\:feed\/channel\/id\/|\d+/', $string, $matches);


$last_digits = ???    

也许有更简单的方法可以做到这一点?

这应该工作。

$aParts = explode('/', $string);
$iId = end($aParts);

就在这里。 在字符串的末尾使用锚点:

preg_match('/\d+$/', $string, $matches);

$表示字符串或多行模式下一行的结尾)

然后,您可以像这样检索ID:

$last_digits = $matches[0];
preg_match('/(\d+)$/', $string, $matches);

$-表示结束

$ matches [1]将具有您的价值

这应该工作:

$string = "tag:domain.com,2012-10-12:feed/channel/id/335";
$pattern = "/\/(\d+)$/";
preg_match($pattern, $string, $matches);

$number = $matches[1];

基本上,您要求输入/到字符串$的末尾之间的任何数字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM