Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting last two characters from a numeric string

Tags:

php

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..

like image 534
Kinz Avatar asked Mar 13 '12 02:03

Kinz


2 Answers

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

Will store "93" in the array $end

like image 90
j08691 Avatar answered Sep 28 '22 14:09

j08691


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

$last2 = $str%100;
like image 34
capi Avatar answered Sep 28 '22 13:09

capi