使用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);
谢谢,伙计,它帮了我的忙