Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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_Parsing - Fatal编程技术网

在php中解析正则表达式模式时出错

在php中解析正则表达式模式时出错,php,regex,parsing,Php,Regex,Parsing,我想拆分一个字符串,例如(通过像“~@”(仅此而已)这样的分隔符): 放入一个数组,其中包含,例如: to => enquiry@test.com subject => test text => this is body/text date => date 我使用的是php5,我有以下正则表达式,几乎可以工作,但有几个错误,必须有一种方法一次性完成: //Split the string in the url of $text at every ~@@

我想拆分一个字符串,例如(通过像“~@”(仅此而已)这样的分隔符):

放入一个数组,其中包含,例如:

to => enquiry@test.com
subject => test
text => this is body/text
date => date 
我使用的是php5,我有以下正则表达式,几乎可以工作,但有几个错误,必须有一种方法一次性完成:

        //Split the string in the url of $text at every ~@@
        $regexp = "/(?:|(?<=~@@))(.*?=.*?)(?:~@@|$|\/(?!.*~@@))/";
        preg_match_all($regexp, $text, $a); 
        //$a[1] is an array containing var1=content1 var2=content2 etc;

        //Now create an array in the form [var1] = content, [var2] = content2
        foreach($a[1] as $key => $value) {
            //Get the two groups either side of the equals sign
            $regexp = "/([^\/~@@,= ]+)=([^~@@,= ]+)/";
            preg_match_all($regexp, $value, $r); 

            //Assign to array key = value
            $val[$r[1][0]] = $r[2][0]; //e.g. $val['subject'] = 'hi'
        }

        print_r($val);
  • 我正在做多个不同的正则表达式搜索,我怀疑我可以做一个

  • 任何帮助都将不胜感激


    谢谢

    像这样分解有很多简单的方法,为什么要使用正则表达式呢

    $str = 'to=enquiry@test.com~@@subject=test~@@text=this is body/text~@@date=date';
    
    $array = explode('~@@',$str);
    $finalArr = array();
    
    foreach($array as $val)
    {
        $tmp = explode('=',$val);
        $finalArr[$tmp['0']] = $tmp['1'];
    }
    
    echo '<pre>';
    print_r($finalArr);
    
    $str='到=enquiry@test.com~@@subject=test~@@text=这是body/text~@@date=date';
    $array=explode(“~@@”,$str);
    $finalArr=array();
    foreach($val形式的数组)
    {
    $tmp=explode('=',$val);
    $finalArr[$tmp['0']]=$tmp['1'];
    }
    回声';
    打印(最终);
    
    确切的字符串是什么?请清楚地显示出来谢谢!你完全正确。我不知道为什么。。。只是给自己制造麻烦。是的,这比尝试
    (?:(?!~@)。*?)(?=~@$)
    要好得多@斯特里比雪夫:没错。
    to => enquiry
    subject => test
    text => this is body/text
    
    $str = 'to=enquiry@test.com~@@subject=test~@@text=this is body/text~@@date=date';
    
    $array = explode('~@@',$str);
    $finalArr = array();
    
    foreach($array as $val)
    {
        $tmp = explode('=',$val);
        $finalArr[$tmp['0']] = $tmp['1'];
    }
    
    echo '<pre>';
    print_r($finalArr);