Is it possible to explode a string like pagination?

For instance:

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

And then explode it like pagination, for example, 8 values ​​each line will result in two 2 pages:

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

Or perhaps explode, for example, by 5 values, each line will be displayed on 3 pages:

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

Where p_explode will be:

p_explode(string_delimiter, string_string, int_number_values_each_page)

Is it possible?

+4
source share
3 answers

Use functions array_chunk()and explode()PHP:

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

Output:

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() splits an array into pieces.

explode()splits a string into a string, in this case create an array of all the numbers contained in $string, dividing them by ,.

+5
source

There are several ways to accomplish your task.

# 1: preg_split() ( ) ( )

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)

Input:

$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 , (preg_quote() ).

+2

check it out 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);
0
source

Source: https://habr.com/ru/post/1692637/


All Articles