Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php从文本文件中读取逗号分隔的值_Php_File - Fatal编程技术网

使用php从文本文件中读取逗号分隔的值

使用php从文本文件中读取逗号分隔的值,php,file,Php,File,我有一个由逗号分隔的内容组成的文本文件 让我的文件为test.txt。内容是: text1, text2, text3 我想读取这个文件并将这三个值赋给3个变量 我已经选中了readfile(文件名,包括路径,上下文)试试这个 $textCnt = "text.txt"; $contents = file_get_contents($textCnt); $arrfields = explode(',', $contents); echo $arrfields[0]; echo $arrf

我有一个由逗号分隔的内容组成的文本文件

让我的文件为
test.txt
。内容是:

text1, text2, text3
我想读取这个文件并将这三个值赋给3个变量

我已经选中了
readfile(文件名,包括路径,上下文)

试试这个

$textCnt  = "text.txt";
$contents = file_get_contents($textCnt);
$arrfields = explode(',', $contents);

echo $arrfields[0]; 
echo $arrfields[1]; 
echo $arrfields[2]; 
或者,在循环中:

foreach($arrfields as $field) {
    echo $field;
}
试试这个

$textCnt  = "text.txt";
$contents = file_get_contents($textCnt);
$arrfields = explode(',', $contents);

echo $arrfields[0]; 
echo $arrfields[1]; 
echo $arrfields[2]; 
或者,在循环中:

foreach($arrfields as $field) {
    echo $field;
}
另一个选项是在不需要分解值的地方使用

如果您的csv文件使用不同的分隔符,只需将其作为参数传递给
fgetcsv

范例

$h = fopen("csv.csv", "r");

while (($line = fgetcsv($h)) !== FALSE) {
    print_r($line);
}

fclose($h);
另一个选项是在不需要分解值的地方使用

如果您的csv文件使用不同的分隔符,只需将其作为参数传递给
fgetcsv

范例

$h = fopen("csv.csv", "r");

while (($line = fgetcsv($h)) !== FALSE) {
    print_r($line);
}

fclose($h);

谢谢,伙计,它帮了我的忙