如何preg_split一个字符串?

时间:2013-01-18 10:01:47

标签: php

我希望使用preg_split:

将数字显示为电话号码
$number = '1234567890';
// preg_split
$number = '123 456 7890';

e.g。也许:

implode(' ', preg_split('/pattern/', $number))

2 个答案:

答案 0 :(得分:4)

$number = '1234567890';
$number = preg_replace('/(\d{3})(\d{3})(\d{4})/','$1 $2 $3',$number);

答案 1 :(得分:0)

试试这个 -

$str = "1234567890";
$split = str_split($str, 3);
echo $split[0]." ".$split[1]." ".$split[2].$split[3];

OR

     $number= "1234567890";
    echo substr($number,0,3)." ".substr($number,3,3)." ".substr($number,5);