我怎样才能得到23
而不是1
for $lastnum1
?
$text = "1 out of 23";
$lastnum1 = $this->getEval(eregi_replace("[^* out of]", '', $text));
你可以这样做:
$text = "1 out of 23";
if(preg_match_all('/\d+/', $text, $numbers))
$lastnum = end($numbers[0]);
$text = "1 out of 23";
$ex = explode(' ',$text);
$last = end($ex);
如果你想确定最后一个是数字
if (is_numeric(end($ex))) {
$last = end($ex);
}
另一种方法:
$text = "1 out of 23";
preg_match('/(\d+)\D*$/', $text, $m);
$lastnum = $m[1];
这将匹配字符串中的最后一个数字,即使它后面跟着非数字。
用于preg_match
将值提取到$matches
:
preg_match("/([0-9]+) out of ([0-9]+)/", $text, $matches);
$text = '1 out of 23';
preg_match('/\d+ out of (\d+)/', $text, $matches);
$lastnum1 = $matches[1];
如果格式相同,为什么不分解字符串并转换最后一个?
<?php
$text = "1 out of 23";
$words = explode(" ",$text);
$lastnum = (int)array_pop($words);