Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.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_Arrays - Fatal编程技术网

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

Php 将字符串转换为固定长度的数组,php,arrays,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..

我有一个字符串,我想转换成一个由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.....

尝试将字符串拆分为数组

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

输出:


我建议学习数组,并建议寻找其他学习来源,而不是
$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..
)