Kinz
Kinz

Reputation: 310

Extracting last two characters from a numeric string

Okay. Say I have string

'193'

And I want to remove the last numbers and store them in an array so I can do operations with them. I know substr can delete the 2 characters, but I'm not sure how to store them after they've been removed..

Upvotes: 5

Views: 32190

Answers (4)

capi
capi

Reputation: 1453

Why not treat it as a number (your question said it's a numeric string) ?

$last2 = $str%100;

Upvotes: 5

Kieran Andrews
Kieran Andrews

Reputation: 5885

$str = "193";
$str_array = str_split($str); 

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

Upvotes: 0

kingjeffrey
kingjeffrey

Reputation: 15290

$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'

Upvotes: 0

j08691
j08691

Reputation: 207953

$end[] = substr("193", -2);

Will store "93" in the array $end

Upvotes: 33

Related Questions