php explode()分隔符问题

php explode()分隔符问题,php,arrays,Php,Arrays,我是PHP新手,因此这可能是一个简单的修复方法。 我有一个名为textt.txt的文本文件,如下所示: <?php $x = file_get_contents('textt.txt'); $y = explode("\r\n", $x); $z = $y[0]; echo $z; ?> 东南亚,2222,代码1 寒假,3333,代码2 我的PHP代码如下所示: <?php $x = file_get_con

我是PHP新手,因此这可能是一个简单的修复方法。
我有一个名为
textt.txt
的文本文件,如下所示:

<?php       
$x = file_get_contents('textt.txt');
        $y = explode("\r\n", $x);
        $z = $y[0];
    echo $z;
?>
东南亚,2222,代码1
寒假,3333,代码2

我的PHP代码如下所示:

<?php       
$x = file_get_contents('textt.txt');
        $y = explode("\r\n", $x);
        $z = $y[0];
    echo $z;
?>

其结果是:

东南亚,2222,代码1

我只希望它返回:

东南亚

我怎样才能做到这一点呢?

explode()。对
explode()
的第一次调用将字符串按行分割,每行包含一个逗号分隔的字符串

<?php       
$x = file_get_contents('textt.txt');
        $y = explode("\r\n", $x);

        // $y[0] now contains the first line "South East asia,2222,code1"
        // explode() that on ","
        $parts = explode(",", $y[0]);

        // And retrieve the first array element
        $z = $parts[0];
    echo $z;
?>

在逗号上再次分解()。对
explode()
的第一次调用将字符串按行分割,每行包含一个逗号分隔的字符串

<?php       
$x = file_get_contents('textt.txt');
        $y = explode("\r\n", $x);

        // $y[0] now contains the first line "South East asia,2222,code1"
        // explode() that on ","
        $parts = explode(",", $y[0]);

        // And retrieve the first array element
        $z = $parts[0];
    echo $z;
?>

快速和肮脏:

<?php       
$all_file = file_get_contents('textt.txt');
$lines = explode("\r\n", $all_file);
$first_line = $lines[0];
$items = explode(",", $first_line);
echo $item[0];
快脏:

<?php       
$all_file = file_get_contents('textt.txt');
$lines = explode("\r\n", $all_file);
$first_line = $lines[0];
$items = explode(",", $first_line);
echo $item[0];

东南亚
2222,code1

寒假
3333
code 2

$x = file_get_contents('textt.txt');
list($z) = explode(",", $x);
echo $z;

东南亚
2222,code1

寒假
3333
code 2

$x = file_get_contents('textt.txt');
list($z) = explode(",", $x);
echo $z;

我认为您要做的是像您一样拆分它,\r\n然后在数组中循环,并在逗号处分解它,只得到区域:

<?php       
$file = file_get_contents('textt.txt');
$fileArray = explode("\r\n", $file);

foreach($fileArray as $value) {
    $region = explode(",", $value);
    echo $region[0] . "<br />\n";
}
?>

我认为您要做的是像您一样拆分它,\r\n然后在数组中循环并用逗号分解它,只得到区域:

<?php       
$file = file_get_contents('textt.txt');
$fileArray = explode("\r\n", $file);

foreach($fileArray as $value) {
    $region = explode(",", $value);
    echo $region[0] . "<br />\n";
}
?>

根据您的用例,您可能不希望一次读取整个文件:

$fp = fopen('textt.txt', 'r');
list($z) = fgetcsv($fp);

根据您的使用情况,您可能不希望一次读取整个文件:

$fp = fopen('textt.txt', 'r');
list($z) = fgetcsv($fp);