Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用点分隔字符串的格式_Php - Fatal编程技术网

Php 用点分隔字符串的格式

Php 用点分隔字符串的格式,php,Php,是否有另一种方法(更小)来格式化用点分隔的字符串?像这样: 未格式化: 9211fe01c98c8847c1a397a6c9c95986 格式化 9211.fe01.c98c.8847.c1a3.97a6.c9c9.5986 我正在使用substr函数,如以下示例: $part1 = substr($codigo, 0, 4); $part2 = substr($codigo, 4, 4); $part3 = substr($codigo, 8, 4); $part4 = substr($

是否有另一种方法(更小)来格式化用点分隔的字符串?像这样:

未格式化: 9211fe01c98c8847c1a397a6c9c95986

格式化 9211.fe01.c98c.8847.c1a3.97a6.c9c9.5986

我正在使用substr函数,如以下示例:

$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 "$part1.$part2.$part3.$part4.$part5.$part6.$part7.$part8"; 
字符串有32个字符。

使用和:

使用和:

$str = '9211fe01c98c8847c1a397a6c9c95986';
$result = implode('.', str_split($str, 4));
$a = "9211fe01c98c8847c1a397a6c9c95986";
echo preg_replace ('/(.{4})(?=.)/', '\\1-', $a);
$arr = implode('.',str_split($codigo,4));