How can I split a delimited string into an array in PHP?

前端 未结 10 1649
失恋的感觉
失恋的感觉 2020-11-21 11:11

I need to split my string input into an array at the commas.

How can I go about accomplishing this?

Input:

9,admin@example.com,8
10条回答
  •  难免孤独
    2020-11-21 11:58

    In simple way you can go with explode($delimiter, $string);

    But in a broad way, with Manual Programming :

            $string = "ab,cdefg,xyx,ht623";
            $resultArr = [];
            $strLength = strlen($string);
            $delimiter = ',';
            $j = 0;
            $tmp = '';
            for ($i = 0; $i < $strLength; $i++) {
                if($delimiter === $string[$i]) {
                    $j++;
                    $tmp = '';
                    continue;
                }
                $tmp .= $string[$i];
                $resultArr[$j] = $tmp;
            }
    

    Outpou : print_r($resultArr);

    Array
    (
        [0] => ab
        [1] => cdefg
        [2] => xyx
        [3] => ht623
    )
    

提交回复
热议问题