I need to isolate the latest occurring integer in a string containing multiple integers.
How can I get 23
instead of 1
for $lastnum1
?
$text = "1 out of 23";
$lastnum1 = $this->getEval(eregi_replace("[^* out of]", '', $text));
strrchr returns the portion of the string after and including the given char, not a numeric index. So you would want to do $last_section = substr(strrchr($string, '. '), 1); to get everything after the char.
The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.
echo substr($mynumber, 0, 2);
you could do:
$text = "1 out of 23";
if(preg_match_all('/\d+/', $text, $numbers))
$lastnum = end($numbers[0]);
Note that
$numbers[0]
contains array of strings that matched full pattern,
and$numbers[1]
contains array of strings enclosed by tags.
$text = "1 out of 23";
$ex = explode(' ',$text);
$last = end($ex);
and if you whant to be sure that that last is a number
if (is_numeric(end($ex))) {
$last = end($ex);
}
Another way to do it:
$text = "1 out of 23";
preg_match('/(\d+)\D*$/', $text, $m);
$lastnum = $m[1];
This will match last number from the string even if it is followed by non digit.
Use preg_match
to extract the values into $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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With