[英]How to extract the last two digits of a number in php?
I have this number 12455, I want to extract the last two digits like $result = 55 .我有这个数字 12455,我想提取最后两位数字,如 $result = 55 。
I tried :我试过 :
$order_id = 12455;
$lastTwoNumbers = $order_id[strlen($order_id)-2];
您可以使用以下代码:
$lastTwoNumbers = substr($order_id, -2);
You can use substr() to get the last two chars of that string (use a negative " start " parameter to count from the end):您可以使用substr()获取该字符串的最后两个字符(使用负的“ start ”参数从末尾开始计数):
<?php
$order_id = 12455;
$lastTwoNumbers = (int) substr($order_id, -2);
echo $lastTwoNumbers;
Will return 55. (The result will be of type string, (int)
makes sure it will be integer after)将返回 55。(结果将是字符串类型, (int)
确保它之后是整数)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.