PHP中字符串补齐为定长

方法一

for ($i=0; $i<100; $i++) {

        $index = sprintf('%04d', $i);
	echo $index.'<br />';
}

方法二

for ($i=0; $i<100; $i++) {

	$index = str_pad($i, 4, 0, STR_PAD_BOTH); // 默认:STR_PAD_RIGHT
	echo $index.'<br />';
}

  

原文地址:https://www.cnblogs.com/intval/p/3622513.html