【问题标题】:Is it possible to explode string like a pagination?是否可以像分页一样爆炸字符串?
【发布时间】:2018-01-24 13:02:44
【问题描述】:

例如:

$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)

有可能吗?

【问题讨论】:

  • 我更喜欢使用range function,请查看 PHP 文档。您可以implode range 函数生成的值。

标签: php arrays explode


【解决方案1】:

使用array_chunk()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 中包含的所有数字除以, 组成的数组。

【讨论】:

    【解决方案2】:

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

    方法#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() 可以解决这些问题)。

    【讨论】:

      【解决方案3】:

      测试一下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);
      

      【讨论】:

      • 这不会提供所需的结果。请重新阅读问题。两个现有答案已经建议使用array_chunk()。请不要发布重复的方法。
      • 建议内爆先接收胶水参数。
      • 这与我的方法 #2 相同,只是您已将 array_map 替换为 for 循环。此外,最好缓存$count,而不是在循环的每次迭代中调用它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多