4

For example:

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

And then explode it like a pagination, for example 8 values each row 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 maybe explode for example into 5 values each row will result into 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 would be:

p_explode(string_delimiter, string_string, int_number_values_each_page)

Is it possible?

Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
Diogo Garcia
  • 536
  • 1
  • 8
  • 20
  • 1
    I'd prefer using the [range function](http://php.net/manual/en/function.range.php) instead, take a look at the PHP doc. You can `implode` the values the range function generates. – xander Jan 24 '18 at 13:04

3 Answers3

5

Use the array_chunk() and explode() PHP's functions:

$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 chunks.

explode() splits a string by string, in that case create an array made by all numbers contained in $string dividing them by ,.

user2342558
  • 5,567
  • 5
  • 33
  • 54
2

There are several ways to accomplish your task.

Method #1: preg_split() (most concise) (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")
}

Method #2: array functions (most stable)

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
    );
}

Method #3: string functions (just for comparison's sake)

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;
}

All methods, will provide the same output from the following input. (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'
];

Call:

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

Output:

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',
)

*Note: The regex method assumes that the delimiter is only one character and is not misinterpreted by regex as a character with special meaning (preg_quote() can fix these issues).

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

test it 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);
Ali Faris
  • 17,754
  • 10
  • 45
  • 70
  • This does not provide the desired result. Please re-read the question. Suggesting to use `array_chunk()` has already been done by the two existing answers. Please do not post duplicate methods. – mickmackusa Jan 24 '18 at 13:40
  • It's recommended for implode to receive the glue argument first. – Progrock Jan 24 '18 at 14:42
  • This is the same method as my Method #2 except you have replaced `array_map` with a `for` loop. Also, it is better practice to cache `$count` rather than call it on each iteration of your loop. – mickmackusa Jan 24 '18 at 14:46