316

我需要将我的字符串输入拆分为逗号处的数组。

有没有办法将逗号分隔的字符串分解成一个扁平的索引数组?

输入:

9,admin@example.com,8

输出:

['9', 'admin@example', '8']  
4

8 回答 8

657

尝试爆炸

$myString = "9,admin@example.com,8";
$myArray = explode(',', $myString);
print_r($myArray);

输出 :

Array
(
    [0] => 9
    [1] => admin@example.com
    [2] => 8
)
于 2009-07-14T14:24:03.593 回答
41
$string = '9,admin@google.com,8';
$array = explode(',', $string);

对于更复杂的情况,您可能需要使用preg_split.

于 2009-07-14T14:24:11.627 回答
32

如果该字符串来自 csv 文件,我会使用fgetcsv()(或者str_getcsv()如果您有 PHP V5.3)。这将允许您正确解析引用的值。如果不是csv,explode()应该是最好的选择。

于 2009-07-14T14:32:10.537 回答
4

如果您希望零件包含逗号怎么办?好吧,引用他们。那么引号呢?好吧,把它们加倍。换句话说:

part1,"part2,with a comma and a quote "" in it",part3

PHP 提供了https://php.net/str_getcsv函数来解析字符串,就好像它是 CSV 文件中的一行一样,可以与上面的行一起使用,而不是explode

print_r(str_getcsv('part1,"part2,with a comma and a quote "" in it",part3'));
Array
(
    [0] => part1
    [1] => part2,with a comma and a quote " in it
    [2] => part3
)
于 2020-07-06T23:06:32.430 回答
2

explode在实际使用中有一些非常大的问题:

count(explode(',', null)); // 1 !! 
explode(',', null); // [""] not an empty array, but an array with one empty string!
explode(',', ""); // [""]
explode(',', "1,"); // ["1",""] ending commas are also unsupported, kinda like IE8

这就是为什么我更喜欢preg_split

preg_split('@,@', $string, NULL, PREG_SPLIT_NO_EMPTY)

整个样板:

/** @brief wrapper for explode
 * @param string|int|array $val string will explode. '' return []. int return string in array (1 returns ['1']). array return itself. for other types - see $as_is
 * @param bool $as_is false (default): bool/null return []. true: bool/null return itself.
 * @param string $delimiter default ','
 * @return array|mixed
 */
public static function explode($val, $as_is = false, $delimiter = ',')
{
    // using preg_split (instead of explode) because it is the best way to handle ending comma and avoid empty string converted to ['']
    return (is_string($val) || is_int($val)) ?
        preg_split('@' . preg_quote($delimiter, '@') . '@', $val, NULL, PREG_SPLIT_NO_EMPTY)
        :
        ($as_is ? $val : (is_array($val) ? $val : []));
}
于 2020-05-13T13:26:21.280 回答
0

使用explode() 或preg_split() 函数用给定的分隔符分割php 中的字符串

// Use preg_split() function 
$string = "123,456,78,000";  
$str_arr = preg_split ("/\,/", $string);  
print_r($str_arr); 
  
// use of explode 
$string = "123,46,78,000"; 
$str_arr = explode (",", $string);  
print_r($str_arr); 
于 2020-08-06T17:37:47.517 回答
0

如果有人想使用 for-each 将逗号分隔的字符串转换为列表项,那么它将帮助您... 此代码用于刀片模板

@php
$data = $post->tags;
$sep_tag= explode(',', $data);
@endphp

@foreach ($sep_tag as $tag)
 <li class="collection-item">{{ $tag}}</li>
 @endforeach 
于 2021-07-23T11:13:59.753 回答
0
//Re-usable function
function stringToArray($stringSeperatedCommas)
{
    return collect(explode(',', $stringSeperatedCommas))->map(function ($string) {
        return trim($string) != null ? trim($string) : null;
    })->filter(function ($string) {
        return trim($string) != null;
    });
}

//Usage
$array = stringToArray('abcd, , dsdsd, dsds');
print($array);

//Result
{ "abcd", "dsdsd", "dsds" }
于 2021-08-16T12:10:17.233 回答