【问题标题】:Converting a string into an array of fixed length将字符串转换为固定长度的数组
【发布时间】:2015-08-20 09:09:39
【问题描述】:

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

【问题讨论】:

标签: php arrays


【解决方案1】:

尝试将字符串拆分为数组

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

【讨论】:

    【解决方案2】:

    只需将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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 2012-02-23
      • 1970-01-01
      相关资源
      最近更新 更多