将php字符串拆分成不同长度的块

时间:2022-06-04 02:25:23

I am looking for ways to split a string into an array, sort of str_split(), where the chunks are all of different sizes.

我正在寻找将字符串拆分为数组的方法,类似于str_split(),其中块的大小都不同。

I could do that by looping through the string with a bunch of substr(), but that looks neither elegant nor efficient. Is there a function that accept a string and an array, like (1, 18, 32, 41, 108, 125, 137, 152, 161), and yields an array of appropriately chopped string pieces?

我可以通过使用一堆substr()循环遍历字符串来做到这一点,但这看起来既不优雅也不高效。是否有一个函数接受一个字符串和一个数组,如(1,18,32,41,108,125,137,152,161),并产生一个适当切碎的字符串数组?

Explode is inappropriate because the chunks are delimited by varying numbers of white spaces.

爆炸是不合适的,因为块是由不同数量的空格分隔的。

4 个解决方案

#1


7  

There is nothing in PHP that will do that for you (it's a bit specific). So as radashk just siad, you just have to write a function

PHP中没有任何内容可以帮助您(这有点具体)。所以作为radashk只是siad,你只需要编写一个函数

function getParts($string, $positions){
    $parts = array();

    foreach ($positions as $position){
        $parts[] = substr($string, 0, $position);
        $string = substr($string, $position);
    }

    return $parts;
}

Something like that. You can then use it wherever you like, so it's clean:

这样的事情。然后你可以在任何你喜欢的地方使用它,所以它很干净:

$parts = getParts('some string', array(1, ... 161));

If you really wanted to, you could implode it into a regular expression:

如果你真的想要,你可以将它破坏成正则表达式:

^.{1}.{18} <lots more> .{161}$

would match what you wanted.

会匹配你想要的。

#2


2  

Just for future references the regular expressions method @jay suggested goes like this:

仅供将来参考,正则表达式方法@jay建议如下:

 $regex="/(.{1})(.{18})(.{32})(.{41})(.{108})(.{125})(.{137})(.{152})(.{161})/";
 preg_match($regex,$myfixedlengthstring,$arr);

where $myfixedlengthstring is the target string and $arr gives you the result

其中$ myfixedlengthstring是目标字符串,$ arr为您提供结果

#3


0  

A slightly more flexible variant, useful to parse ASCII tables with fixed-length records:

一个稍微灵活的变体,用于解析具有固定长度记录的ASCII表:

function ParseIrregularString  ($string, $lengths)
{ 
$parts = array(); 

foreach ($lengths as $StringKey => $position)
    { 
    $parts[$StringKey] = substr($string, 0, $position); 
    $string = substr($string, $position); 
    } 

return $parts; 
} 

Submitting the following:

提交以下内容:

$string = "abcdefghiklmnopqrstuvz";
$lengths = array ("field1"=>4, "field2"=>3, "field3"=>5, "field4"=>2);
print_r (ParseIrregularString ($string, $lengths));

returns:

Array ( [field1] => abcd [field2] => efg [field3] => hiklm [field4] => no )

#4


0  

I would use unpack():

我会使用unpack():

$str = "?ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890....";
$array = unpack("a1first/a26second/a10third/a*rest",$str);
print_r($array);

This returns:

Array
(
    [first] => ?
    [second] => ABCDEFGHIJKLMNOPQRSTUVWXYZ
    [third] => 1234567890
    [rest] => ....
)

#1


7  

There is nothing in PHP that will do that for you (it's a bit specific). So as radashk just siad, you just have to write a function

PHP中没有任何内容可以帮助您(这有点具体)。所以作为radashk只是siad,你只需要编写一个函数

function getParts($string, $positions){
    $parts = array();

    foreach ($positions as $position){
        $parts[] = substr($string, 0, $position);
        $string = substr($string, $position);
    }

    return $parts;
}

Something like that. You can then use it wherever you like, so it's clean:

这样的事情。然后你可以在任何你喜欢的地方使用它,所以它很干净:

$parts = getParts('some string', array(1, ... 161));

If you really wanted to, you could implode it into a regular expression:

如果你真的想要,你可以将它破坏成正则表达式:

^.{1}.{18} <lots more> .{161}$

would match what you wanted.

会匹配你想要的。

#2


2  

Just for future references the regular expressions method @jay suggested goes like this:

仅供将来参考,正则表达式方法@jay建议如下:

 $regex="/(.{1})(.{18})(.{32})(.{41})(.{108})(.{125})(.{137})(.{152})(.{161})/";
 preg_match($regex,$myfixedlengthstring,$arr);

where $myfixedlengthstring is the target string and $arr gives you the result

其中$ myfixedlengthstring是目标字符串,$ arr为您提供结果

#3


0  

A slightly more flexible variant, useful to parse ASCII tables with fixed-length records:

一个稍微灵活的变体,用于解析具有固定长度记录的ASCII表:

function ParseIrregularString  ($string, $lengths)
{ 
$parts = array(); 

foreach ($lengths as $StringKey => $position)
    { 
    $parts[$StringKey] = substr($string, 0, $position); 
    $string = substr($string, $position); 
    } 

return $parts; 
} 

Submitting the following:

提交以下内容:

$string = "abcdefghiklmnopqrstuvz";
$lengths = array ("field1"=>4, "field2"=>3, "field3"=>5, "field4"=>2);
print_r (ParseIrregularString ($string, $lengths));

returns:

Array ( [field1] => abcd [field2] => efg [field3] => hiklm [field4] => no )

#4


0  

I would use unpack():

我会使用unpack():

$str = "?ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890....";
$array = unpack("a1first/a26second/a10third/a*rest",$str);
print_r($array);

This returns:

Array
(
    [first] => ?
    [second] => ABCDEFGHIJKLMNOPQRSTUVWXYZ
    [third] => 1234567890
    [rest] => ....
)