fish man
fish man

Reputation: 2720

php split a number with 2 part, Single digit and the rest

Is there any good way to split a number with 2 part, Single digit and the rest?

123456 => 12345   6
789 => 78  9
12357 =? 1235 7

My idea is first use strlen() to get number lenght then use substr() to get Single digit and the rest.

Anyone have better idae? more easy way? Thanks.

Upvotes: 0

Views: 235

Answers (3)

UltraInstinct
UltraInstinct

Reputation: 44474

If the input originally is a number, then why not:

$num = 123456;
echo floor($num / 10); //first part
echo $num % 10;        // second part

Upvotes: 1

Ryan
Ryan

Reputation: 3582

To get both sections, I would use the following strlen calls.

$first  = substr($string, 0, -1);
$second = substr($string, -1);

Upvotes: 0

Maarkoize
Maarkoize

Reputation: 2621

Try only

substr($yourStringorNumber,-1)

to get the last sign/digit. You can get the other part with

substr($yourStringorNumber,0,-1)

Upvotes: 0

Related Questions