我想分割一个字符串,如下所示(通过像'〜@''这样的分隔符(只有那个)):
to=enquiry@test.com~@@subject=test~@@text=this is body/text~@@date=date
到包含例如:
的数组中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);
我的疑问是:
它似乎没有捕获超过3组不同的参数
@符号正在破坏,因此不会捕获电子邮件地址,例如返回:
to => enquiry
subject => test
text => this is body/text
我正在做多个不同的正则表达式搜索,我怀疑我能做一个。
任何帮助都会非常感激。
由于
答案 0 :(得分:0)
为什么你使用正则表达式时有很多简单的方法来执行$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);
这样的
application.js