在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);