将字符串转换为固定长度的数组

时间:2015-08-20 09:09:39

标签: php arrays

我有一个字符串,我想将其转换为包含6个元素的数组。

 $x=Address : "MK/LK G8, 2ND FLR, MALAL VISO INFO 19-Aug-15 Acct Number :   _254566003 etc...

如果我想要一个数组

$parts[0]=MK/LKG......Length should be 6 (whitespace wont consider)
$parts[1]=82NDFLRLength should be 6
$parts[2]=etc.....

2 个答案:

答案 0 :(得分:0)

尝试将字符串拆分为数组

$str = "MK/LK G8, 2ND FLR, MALAL VISO INFO 19-Aug-15 Acct Number : _254566003 etc..."
$arr = str_split($str, 6);

答案 1 :(得分:0)

只需使用str_splitpreg_replace作为

$x="MK/LK G8, 2ND FLR, MALAL VISO INFO 19-Aug-15 Acct Number : _254566003 etc..";
$res = str_split(preg_replace('/\s/','',$x),6);
print_r($res);

<强>输出:

Array
(
    [0] => MK/LKG
    [1] => 8,2NDF
    [2] => LR,MAL
    [3] => ALVISO
    [4] => INFO19
    [5] => -Aug-1
    [6] => 5AcctN
    [7] => umber:
    [8] => _25456
    [9] => 6003et
    [10] => c..
)

Demo