str_getcsv

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

str_getcsv 解析 CSV 字符串为一个数组

说明

str_getcsv(
    string $string,
    string $separator = ",",
    string $enclosure = "\"",
    string $escape = "\\"
): array

CSV 字段格式解析字符串输入,并返回包含读取字段的数组。

注意:

此函数考虑区域设置。如果 LC_CTYPE 是类似 en_US.UTF-8 的值,此函数将错误的读取单字节编码的字符串。

参数

string

待解析的字符串。

separator

设定字段界定符(仅一个单字节字符)。

enclosure

设定字段包裹字符(仅一个单字节字符)。

escape

设置转义字符(最多一个单字节字符)。默认为反斜线(\)。空字符串("")禁用专有转义机制。

注意: Usually an enclosure character is escaped inside a field by doubling it; however, the escape character can be used as an alternative. So for the default parameter values "" and \" have the same meaning. Other than allowing to escape the enclosure character the escape character has no special meaning; it isn't even meant to escape itself.

返回值

返回一个包含读取到的字段的索引数组。

更新日志

版本 说明
7.4.0 escape 现在将空字符串视为禁用专有转义机制的信号。以前视为默认参数值。

范例

示例 #1 str_getcsv() 示例

<?php

$string
= 'PHP,Java,Python,Kotlin,Swift';
$data = str_getcsv($string);

var_dump($data);
?>

以上例程会输出:

array(5) {
  [0]=>
  string(3) "PHP"
  [1]=>
  string(4) "Java"
  [2]=>
  string(6) "Python"
  [3]=>
  string(6) "Kotlin"
  [4]=>
  string(5) "Swift"
}

参见

  • fgetcsv() - 从文件指针中读入一行并解析 CSV 字段

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top