计算它!
- 致电
strlen()
- 除以
10
- 致电
floor() 删除小数
- 乘以
10
- 检查是否小于
10
这是一系列测试:
代码:(Demo)
$strings=[9=>'123456789',10=>'1234567890',11=>'12345678901',19=>'1234567890123456789',20=>'12345678901234567890',21=>'123456789012345678901'];
foreach($strings as $k=>$str){
if(($group=floor(strlen($str)/10)*10)<10){ // use arithmetic to find the group, declare and check group in one step
echo "$k => $str is not upto 10";
}else{
echo "$k => $str is in group $group";
}
echo "\n";
}
输出:
9 => 123456789 is not upto 10
10 => 1234567890 is in group 10
11 => 12345678901 is in group 10
19 => 1234567890123456789 is in group 10
20 => 12345678901234567890 is in group 20
21 => 123456789012345678901 is in group 20
OP 更新后:
代码:(Demo)
$strings=[9=>'123456789',10=>'1234567890',11=>'12345678901',19=>'1234567890123456789',20=>'12345678901234567890',21=>'123456789012345678901'];
foreach($strings as $k=>$str){
echo "$k => $str is in group ",(floor(strlen($str)/10)+1)*10,"\n";
}
输出:
9 => 123456789 is in group 10
10 => 1234567890 is in group 20
11 => 12345678901 is in group 20
19 => 1234567890123456789 is in group 20
20 => 12345678901234567890 is in group 30
21 => 123456789012345678901 is in group 30
这是另一种完全符合 OP 要求的方法,但我敢打赌它也“不正确”。
http://sandbox.onlinephpfunctions.com/code/77effe7f68c389bafb1d104a49b7055873e7b038