Ответ 1
вы можете сделать:
$text = "1 out of 23";
if(preg_match_all('/\d+/', $text, $numbers))
$lastnum = end($numbers[0]);
Как я могу получить 23
вместо 1
для $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);
}
Используйте 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);
Другой способ сделать это:
$text = "1 out of 23";
preg_match('/(\d+)\D*$/', $text, $m);
$lastnum = $m[1];
Это будет соответствовать последнему номеру строки, даже если за ним следует не цифра.