Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.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_Regex_Reference - Fatal编程技术网

Php 根据逗号将文本文件分解为数组

Php 根据逗号将文本文件分解为数组,php,regex,reference,Php,Regex,Reference,我想将我的text.tx转换为仅依赖逗号的数组,例如: 如果我有这段文字: 所以我需要这样执行数组: 请检查我的代码,我的代码不能正常工作,因为它依赖于逗号和换行符: <?php $lines = file ("text.txt"); foreach($lines as $line) { $data[] = explode(',', $line); } ?> <pre> <?php print_r($data); ?> &

我想将我的text.tx转换为仅依赖逗号的数组,例如:

如果我有这段文字:

所以我需要这样执行数组:

请检查我的代码,我的代码不能正常工作,因为它依赖于逗号和换行符:

<?php
$lines = file ("text.txt");
foreach($lines as $line) {
    $data[] = explode(',', $line);
}
?>
<pre>
    <?php
    print_r($data);
    ?>
</pre>

$handle=fopen(“text.txt”、“r”);
如果($handle){
$data=[];
while(($line=fgets($handle))!==false){
$data[]=分解(“,”,$line);
}
fclose($handle);
回声“;
打印(数据);
}否则{
//打开文件时出错。
} 

不是图片!编辑您的问题并复制粘贴文本。另外,为什么
像这样
像那样
在你的预期结果中,你需要的不仅仅是爆炸,因为
这个
变成了
那个
。你能编辑你的问题来解释你当前的爆炸有什么不好用吗?
$handle = fopen("text.txt", "r");
if ($handle) {
    $data = [];
    while (($line = fgets($handle)) !== false) {
        $data[] = explode(',', $line);
    }

    fclose($handle);
    echo "<pre>";
    print_r($data); 
} else {
    // error opening the file.
}