2011-02-18 79 views
2

我需要php爆炸()的功能,但没有分隔符。例如,将变量“12345”变成一个数组,分别保存每个数字。PHP分裂大数字(如爆炸)

这是可能的吗?我已经使用Google搜索,但只能找到爆炸(),这似乎并不奏效。

谢谢!

回答

7

在PHP任何字符串:

$foo="12345"; 
echo $foo[0];//1 
echo $foo[1];//2 
//etc 

或(从使preg_split())页在手动

$str = 'string'; 
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); 
print_r($chars); 

甚至更​​好:

$str = 'string'; 
$chars=str_split($str, 1) 
print_r($chars); 

基准使preg_split的( )vs str_split()

function microtime_float() 
{ 
    list($usec, $sec) = explode(" ", microtime()); 
    return ((float)$usec + (float)$sec); 
} 


$str = '12345'; 
$time_start = microtime_float(); 
for ($i = 0; $i <100000; $i++) { 
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); 
//$chars=str_split($str, 1); 
} 
$time_end = microtime_float(); 
$time = $time_end - $time_start; 

echo "$time seconds\n"; 

结果:

str_split =0.69 
preg_split =0.9 
+0

哦,王牌!我实际上现在感觉有点愚蠢:)谢谢! – 2011-02-18 02:24:34

4

如果你真的想创建一个数组,然后使用str_split(),即

echo '<pre>'. print_r(str_split("123456", 1), true) .'</pre>'; 

会导致

Array 
(
    [0] => 1 
    [1] => 2 
    [2] => 3 
    [3] => 4 
    [4] => 5 
) 
1

您的电话号码可以变成弦,然后像阵列一样行动

$i = 2342355; $i=(string)$i; 
//or 
$i='234523452435234523452452452'; 

//then 

$i[2]==4 

//numeration started from 0