用PHP添加符号

时间:2011-05-19 10:46:59

标签: php arrays

如何在每4个字符后添加-

示例:

498c665baf79ad6e - > 498c-665b-af79-ad6e

4 个答案:

答案 0 :(得分:11)

echo implode('-', str_split('498c665baf79ad6e', 4));

答案 1 :(得分:1)

<?php

$source = "...";

$hypenated = array();

for ($i = 0; $i < strlen($source)/4; $i++) {
    $hypenated[] = substr($source, $i*4, 4);
}

$hypenated = implode('-', $hypenated);

答案 2 :(得分:1)

$s = '498c665baf79ad6e';
echo preg_replace ('/(.{4})(?=.)/', '\1-', $s);

答案 3 :(得分:1)

或者:

<?php
echo rtrim(chunk_split('498c665baf79ad6e', 4, '-'), '-');