Reputation: 2720
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
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
Reputation: 3582
To get both sections, I would use the following strlen calls.
$first = substr($string, 0, -1);
$second = substr($string, -1);
Upvotes: 0
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