是否可以像分页一样分解字符串?

Is it possible to explode string like a pagination?

例如:

$string = '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15';

然后像分页一样展开它,例如每行 8 个值将导致两个 2 页:

$arr = p_explode(',', $string, 8);
array(
    0 => "1,2,3,4,5,6,7,8"
    1 => "9,10,11,12,13,14,15"
) 

或者可能分解成 5 个值,每行将产生 3 页:

$arr = p_explode(',',$string,5);
array(
    0 => "1,2,3,4,5"
    1 => "6,7,8,9,10"
    2 => "11,12,13,14,15"
) 

其中 p_explode 将是:

p_explode(string_delimiter, string_string, int_number_values_each_page)

可能吗?

使用array_chunk() and explode()PHP的功能:

$elementsPerPage = 5;
$arrayOfPages = array_chunk(explode(',', $string), $elementsPerPage);
print_r($arrayOfPages);

输出:

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

    [1] => Array
        (
            [0] => 6
            [1] => 7
            [2] => 8
            [3] => 9
            [4] => 10
        )

    [2] => Array
        (
            [0] => 11
            [1] => 12
            [2] => 13
            [3] => 14
            [4] => 15
        )

)

array_chunk() 将数组拆分为块。

explode() 逐个字符串拆分,在这种情况下创建一个数组,该数组由 $string 中包含的所有数字除以 ,

有多种方法可以完成您的任务。

方法#1:preg_split()(最简洁)(Pattern Demo)

function p_explode($delim,$string,$max_elements){
    return preg_split('/(?:[^'.$delim.']+\K'.$delim.'){'.$max_elements.'}/',$string);
    //                     ^^^^^^^^^^^^^^-- this can be .+? if the delimiter is more than one character (a dot character representing "any non-newline character")
}

方法#2:数组函数(最稳定)

function p_explode($delim,$string,$max_elements){
    return array_map(
        function($v)use($delim){
            return implode($delim,$v);  // join each subarrays' elements with the delimiter
        },
        array_chunk(explode($delim,$string),$max_elements)  // explode and break into subarrays
    );
}

方法#3:字符串函数(仅供比较)

function p_explode($delim,$string,$max_elements){
    $delim_len=strlen($delim);
    $pos=-1;  // set $pos
    $i=1;  // set $i
    while(false!==$pos=strpos($string,$delim,$pos+1)){  // advance $pos while $delim exists
        if($i<$max_elements){
            ++$i;  // increment ($max_elements-1) times
        }else{
            $result[]=substr($string,0,$pos);  // on ($max_elements)th time, store substring
            $string=substr($string,$pos+$delim_len);  // update $string with what is leftover(after delimiter)
            $pos=-1;  // reset $pos
            $i=1;  // reset $i
        }
    }
    if($i){
        $result[]=$string;  // if anything left, store as final element
    }
    return $result;
}

所有方法,将从以下输入提供相同的输出。 (PHP Demo)

输入:

$strings=[
    '1,2,3,4,5,6,7',
    '1,2,3,4,5,6,7,8',
    '1,2,3,4,5,6,7,8,9,10',
    '1,2,3,4,5,6,7,8,9,10,11,12,13',
    '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15',
    '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18'
];

致电:

foreach($strings as $string){
    var_export(p_explode(',',$string,8));
    echo "\n\n";
}

输出:

array (
  0 => '1,2,3,4,5,6,7',
)

array (
  0 => '1,2,3,4,5,6,7,8',
)

array (
  0 => '1,2,3,4,5,6,7,8',
  1 => '9,10',
)

array (
  0 => '1,2,3,4,5,6,7,8',
  1 => '9,10,11,12,13',
)

array (
  0 => '1,2,3,4,5,6,7,8',
  1 => '9,10,11,12,13,14,15',
)

array (
  0 => '1,2,3,4,5,6,7,8',
  1 => '9,10,11,12,13,14,15,16',
  2 => '17,18',
)

*注意:regex 方法假定分隔符只有一个字符,不会被 regex 误解为具有特殊含义的字符(preg_quote() 可以解决这些问题)。

测试一下 here

function p_explode($string_delimiter, $string_string, $int_number_values_each_page)
{

        $array = explode($string_delimiter, $string_string);
        $result = array_chunk($array, $int_number_values_each_page);

        $count = count($result);
        for($i=0;$i<$count;$i++)
            $result[$i] = implode( "," , $result[$i]);

        return $result;
}


$string = '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15';
$arr = p_explode(',', $string, 8);
var_dump($arr);