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

Php 预匹配所有项目名称和说明

Php 预匹配所有项目名称和说明,php,regex,preg-match-all,Php,Regex,Preg Match All,我有一个.txt文件,其中包含一些如下所示的数据: item_name_MyItem1=Nice Title item_desc_MyItem1=A short description 或 注意:有时只有项目名称,但没有项目描述 我想比赛 我的项目1 如果是描述或名称 之后的一切= 因此,预期的结果是: Array ( [0] => Array ( [0] => item_name_MyItem1=Nice Title

我有一个.txt文件,其中包含一些如下所示的数据:

item_name_MyItem1=Nice Title
item_desc_MyItem1=A short description

注意:有时只有项目名称,但没有项目描述

我想比赛

我的项目1 如果是描述或名称 之后的一切= 因此,预期的结果是:

Array
(
    [0] => Array
        (
            [0] => item_name_MyItem1=Nice Title
            [1] => name
            [2] => MyItem1
            [3] => Nice Title
        )

    [1] => Array
        (
            [0] => item_desc_MyItem1=A short description
            [1] => desc
            [2] => MyItem1
            [3] => A short description
        )

    [2] => Array
        (
            [0] => MyItem2_item_name=Nice Title
            [1] => name
            [2] => MyItem2
            [3] => Nice Title
        )

    [3] => Array
        (
            [0] => MyItem2_item_desc=A short description
            [1] => desc
            [2] => MyItem2
            [3] => A short description
        )

    // For example: MyItem3 only has a item_name

    [4] => Array
        (
            [0] => item_name_MyItem3=Nice Title
            [1] => name
            [2] => MyItem3
            [3] => Nice Title
        )
)
到目前为止,我有这个正则表达式:~?:.*item_name | desc?:.*?=.*i将它与PREG_SET_顺序一起使用

这个正则表达式的问题是,它在每个数组上都有一个空值,因为有选项?:.*?和?:


希望有人能帮助我修复此正则表达式,使其符合我的预期结果。

从第一个可选部分中删除内部捕获组:

.*=>*

所以你有:

~?:.*i项目名称|描述:.*?=.*i

但您可以完全删除该前缀,因为它是可选的,并且您没有任何东西可以捕获:


~item_name | desc?:.*?=.*~i

逐行读取文件并按=


贾斯蒂纳斯的回答让我困惑。它看起来不应该起作用,所以我会发布一些我认为有效的东西

$handle=fopen("inputfile.txt","r");
if($handle){
    while(($line=fgets($handle))!==false){
        $dyad=explode("=",$line);  // split in two parts
        $attrs=explode("_",$dyad[0]);  // split the first part into 3 parts
        if(strpos('item',$attrs[0])===0){
            $result[]=[$attrs[2],$attrs[1],$dyad[1]];
        }else{
            $result[]=[$attrs[0],$attrs[2],$dyad[1]];
        }
    }
    var_export($result);
    fclose($handle);
}

这是一个。

为什么不逐行读取文件并分解等号,然后你可以在等号的左边做一个stripos,看看它是一个名称还是一个描述?我不知道这样是否更快,因为它像25000多行,并不是所有的都是项目名称和描述。它更快,内存效率更高。这些空值有什么问题吗?那么我不知道项目名称。事实上,你应该只为第一个项目做。事实上,你根本不应该有那个内部捕获组,甚至在物品之前也不应该有。在这种情况下,你也可以使用生成器,返回一个重数MB数据的巨大数组会扼杀我们获得的所有收益,因为我们逐行读取文件。经过一点编辑,它会按照我的预期工作。谢谢
$handle = fopen("inputfile.txt", "r");
$results = [];

if ($handle) {
    while (($line = fgets($handle)) !== false) {
        list($name, $desc) = explode('=', $line);

        $results[] = [
            $line,
            strpos($name, 'name') !== false ? 'name' : 'desc',
            $name,
            $desc
        ];
    }

    fclose($handle);
} else {
    // error opening the file.
} 
$handle=fopen("inputfile.txt","r");
if($handle){
    while(($line=fgets($handle))!==false){
        $dyad=explode("=",$line);  // split in two parts
        $attrs=explode("_",$dyad[0]);  // split the first part into 3 parts
        if(strpos('item',$attrs[0])===0){
            $result[]=[$attrs[2],$attrs[1],$dyad[1]];
        }else{
            $result[]=[$attrs[0],$attrs[2],$dyad[1]];
        }
    }
    var_export($result);
    fclose($handle);
}