PHP:根据逗号分隔的X计数将字符串拆分为2个字符串

So, I have a comma delimited string which I want to split into two strings after count of x commas.

So, if my string was as follows:

temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9, temp10

I want to split it into two strings after a count of 4 commas, I would expect:

$string1 = 'temp1, temp2, temp3, temp4, ';

$string2 = 'temp5, temp6, temp7, temp8, temp9, temp10';

How can I achieve this in PHP?

There are lots of ways to do this - one approach would be to explode the string and then implode slices of that array like this:

$x = 4;
$delim = ', ';
$str = 'temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9, temp10';
$parts = explode($delim,$str);
$string1 = implode($delim,array_slice($parts,0,$x)) . $delim;
$string2 = implode($delim,array_slice($parts,$x));

To me, this is the cleanest and most readable way to get the job done and does not require a loop.

One other approach is to iterate on each char:

    $string = "temp1, temp2, temp3, temp4, temp5, temp6, temp7";

    $commacount = 0;
    $stringBefore = "";
    $stringAfter = "";
    $splitHasHappened = false;
    foreach (str_split($string) as $char) {

        if ($splitHasHappened) {
            $stringAfter .= $char;
        }
        else {
            $stringBefore .= $char;
        }

        if ($char == ",") {
            $commacount++;
        }

        if ($commacount == 4) {
            $splitHasHappened = true;
        }

    }

    echo "Before: $stringBefore - After $stringAfter"

You could create a simple Function that takes 2 Arguments (The Comma delimited String and the Index at which to Split. The Result would be a numerically indexed Array with 2 keys: 0 and 1. $array[0] holds the first part and $array1 holds the second part. Below is the function:

     <?php

        $strings    = "temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9, temp10";


        function splitAtIndex($commaString, $index){
            $arrBlended     = array();
            $arrStrings     = preg_split("#,\s?#", $commaString);
            if(!empty($arrStrings)){
                $partB          = array_splice($arrStrings, $index);
                $partA          = $arrStrings;

                $arrBlended[]   = implode(", ", $partA) . ", ";
                $arrBlended[]   = implode(", ", $partB);

            }
            return $arrBlended;
        }

        $arrSplitStrings        = splitAtIndex($strings, 4);
        $string1                = $arrSplitStrings[0]; //<== 'temp1, temp2, temp3, temp4, '
        $string2                = $arrSplitStrings[1]; //<== 'temp5, temp6, temp7, temp8, temp9, temp10' 
        var_dump($arrSplitStrings);

        // DUMPS 
        array (size=2)
          0 => string 'temp1, temp2, temp3, temp4, ' (length=28)
          1 => string 'temp5, temp6, temp7, temp8, temp9, temp10' (length=41)

Test and Confirm the Result HERE:

you can explode it into an array

$array = explode(",",$string);
for($i=0;$i<4;$i++){
  $string1[$i]=$array[$i];
} 
$string1 = implode(",",$string1);
for($i=4;$i<count($array);$i++){
  $string2[$i]=$array[$i];
}
$string2 = implode(",",$string2);

Just for fun. The {4} can be changed or substituted with a variable:

preg_match('/([^,]+,){4}/', $string, $matches);

$string1 = $matches[0];
$string2 = str_replace($string1, '', $string);

I prefer not using a loop for this. You would actually want some checking/validation of the input:

<?php

$count = 4;
$str = 'temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9, temp10';
$match = preg_match('/^([^,]+,\s?){' . $count . '}/', $str, $matches) ? $matches[0] : false;
if ($match) {
    $position = strlen($match);
    $str1 = substr($str, 0, $position);
    $str2 = substr($str, $position);
}

The solution using str_word_count,array_slice, array_filter and substr functions:

$str = "temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9, temp10";

$comma_number = 4;
// finding the position of 4th comma
$pos = key(array_slice(array_filter(str_word_count($str, 2, ","), function($v){
    return $v == ",";
}), $comma_number - 1, 1, true));

$string1 = substr($str, 0, $pos + 2);
$string2 = substr($str, $pos + 2);

var_dump($string1, $string2);

The output:

string(28) "temp1, temp2, temp3, temp4, "
string(41) "temp5, temp6, temp7, temp8, temp9, temp10"

This Great For Me to split the specific comma sperated count values and add the br tag

<?php 
$string='temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9, temp10';
$delim = ',';
$x = 4;
$parts = explode($delim,$string);
$numberofloop=ceil(Count($parts)/$x);
for($i=0,$j=0;$i<$numberofloop;$i++){
        echo implode($delim,array_slice($parts,$j,$x))."<br>";
        $j=$j+$x;   
}
?>