format a string separate with dots
Is there another alternative (smaller) for to format a string separate with dots? Like this:
Unformatted: 9211fe01c98c8847c1a397a6c9c95986
Formatted 9211.fe01.c98c.8847.c1a3.97a6.c9c9.5986
I'm using the substr function, like this sample:
$part1 = substr($codigo, 0, 4);
$part2 = substr($codigo, 4, 4);
$part3 = substr($codigo, 8, 4);
$part4 = substr($codigo, 12, 4);
$part5 = substr($codigo, 16, 4);
$part6 = substr($codigo, 20, 4);
$part7 = substr($codigo, 24, 4);
$part8 = substr($codigo, 28, 4);
echo "$part开发者_C百科1.$part2.$part3.$part4.$part5.$part6.$part7.$part8";
The string have 32 chars.
Use str_split and implode:
$str = '9211fe01c98c8847c1a397a6c9c95986';
$result = implode('.', str_split($str, 4));
$a = "9211fe01c98c8847c1a397a6c9c95986";
echo preg_replace ('/(.{4})(?=.)/', '\\1-', $a);
$arr = implode('.',str_split($codigo,4));
精彩评论