是否有可能像分页一样爆炸字符串?

时间:2018-01-24 13:02:44

标签: php arrays explode

例如:

function runDBQuery($SqlServer, $database, $query){
  $SqlConn = new-object Microsoft.SqlServer.Management.Smo.Server $SQLServer
  $db = $SqlConn.Databases[$database]
  $result = new-object System.Data.DataSet
  $result = $db.ExecuteWithResults($query)
return $result
}

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

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

或者可能爆炸,例如5个值,每行将产生3页:

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

p_explode将在哪里:

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

有可能吗?

3 个答案:

答案 0 :(得分:5)

使用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中包含的所有数字组成的数组,将它们除以,

答案 1 :(得分: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',
)

*注意:正则表达式方法假定分隔符只有一个字符,并且没有被正则表达式误解为具有特殊含义的字符(preg_quote()可以解决这些问题)。

答案 2 :(得分:0)

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