Extract the last two characters from a numeric string

Good. Say I have a line

'193'

And I want to delete the last numbers and store them in an array so that I can perform operations with them. I know substr can remove two characters, but I'm not sure how to save them after deleting them.

+5
source share
4 answers
$end[] = substr("193", -2);

Will store "93" in the $ end array

+32
source

Why not treat it like a number (your question said it was a number string)?

$last2 = $str%100;
+5
source
$array = str_split('193'); // $array now has 3 elements: '1', '9', and '3'
array_shift($array); // this removes '1' from $array and leaves '9' and '3'
0
source
$str = "193";
$str_array = str_split($str); 

$number_1 = array_pop($str_array); //3
$number_2 = array_pop($str_array); //9
0
source

All Articles