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

Php 在小于和大于之间拆分字符串

Php 在小于和大于之间拆分字符串,php,regex,pcre,Php,Regex,Pcre,我需要拆分这类字符串,以便在小于和大于之间分隔电子邮件。我正在尝试下一个regex和preg_split,但我不起作用 ”email1@domain.com" 新闻 一些东西 预期结果将是: 数组 ( [0] => "email1@domain.com" [1] => email@email.com ) 排列 ( [0]=>新闻 [1] => news@e.domain.com ) 排列 ( [0]=>一些东西 [1] =>电子邮件-noreply@somestuff.com ) 我现在使

我需要拆分这类字符串,以便在小于和大于之间分隔电子邮件。我正在尝试下一个
regex
preg_split
,但我不起作用

”email1@domain.com" 
新闻
一些东西
预期结果将是:

数组
(
[0] => "email1@domain.com"
[1] => email@email.com
)
排列
(
[0]=>新闻
[1] => news@e.domain.com
)
排列
(
[0]=>一些东西
[1] =>电子邮件-noreply@somestuff.com
)
我现在使用的代码:

foreach($email作为$email)
{
$pattern='/';
$result=preg_split($pattern,$email);
打印(结果);
}

对某个对象进行拆分会删除分隔符(即正则表达式匹配的所有内容)。你可能想和我分手

\s*<|>
并使用第一个和第二个捕获组。

preg\u match\u all(“//”,$string,$result\u数组);
preg_match_all("/<(.*?)>/", $string, $result_array);
print_r($result_array);
打印(结果数组);
您可以使用以下可用的一些标志:
PREG\u SPLIT\u DELIM\u CAPTURE
PREG\u SPLIT\u NO\u EMPTY

$emails = array('"email1@domain.com" <email1@domain.com>', 'News <news@e.domain.com>', 'Some Stuff <email-noreply@somestuff.com>');

foreach ($emails as $email)
{
    $pattern = '/<(.*?)>/';
    $result = preg_split($pattern, $email, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
    print_r($result);
}
您也可以通过进行拆分,这将完成任务

$header='”email1@domain.com" 
新闻
一些东西';
$result=array();
preg_match_all(“!(.*?\s+!”,$header,$result);
$formatted=array();
对于($i=0;$i$result[1][$i],
'email'=>$result[2][$i],
);
}
打印(格式化);

您想要什么输出?看起来拆分正在工作。我想要的输出是在预期结果为:。现在,拆分不起作用,因为我没有得到任何拆分器。我总是有和原来一样的字符串。这就是我想要得到的。我得到了第二个选项的
preg_match():未知修饰符'>'
preg_split():第一个选项的分隔符不能是字母数字或反斜杠。我给了你一个正则表达式,你需要添加分隔符,以便与PHP中的PCRE函数一起使用。根据你使用的字符串,可能会逃避一些事情。因此,在那里使用它可能应该是
'/^(.*?\s*]+)>/'
。要使用的
$pattern
是什么?与您已经使用的相同。为了清晰起见,我将编辑答案。不起作用。。。我总是得到原始字符串,例如
array(1){[0]=>string(57)”email1@domain.com“”}
在我这方面效果很好。你用的是“原样”代码吗?尝试复制和粘贴,只是为了确保。你是对的,这是有效的。但不是我的阵列。我将试着猜测原因,我想这将是一件愚蠢的事情。谢谢你;)
$emails = array('"email1@domain.com" <email1@domain.com>', 'News <news@e.domain.com>', 'Some Stuff <email-noreply@somestuff.com>');

foreach ($emails as $email)
{
    $pattern = '/<(.*?)>/';
    $result = preg_split($pattern, $email, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
    print_r($result);
}
Array
(
    [0] => "email1@domain.com" 
    [1] => email1@domain.com
)
Array
(
    [0] => News 
    [1] => news@e.domain.com
)
Array
(
    [0] => Some Stuff 
    [1] => email-noreply@somestuff.com
)
$email='"email1@domain.com" <email1@domain.com>
News <news@e.domain.com>
Some Stuff <email-noreply@somestuff.com>';
$pattern = '![^\>\<]+!';
preg_match_all($pattern, $email,$match);
print_r($match);
Array ( [0] => Array ( 
[0] => "email1@domain.com" 
[1] => email1@domain.com 
[2] => News 
[3] => news@e.domain.com 
[4] => Some Stuff 
[5] => email-noreply@somestuff.com ) ) 
  $pattern = '/</';
  $result = preg_split($pattern, $email);
  $result = preg_replace("/>/", "", $result);
$header = '"email1@domain.com" <email1@domain.com>
News <news@e.domain.com>
Some Stuff <email-noreply@somestuff.com>';

$result = array();
preg_match_all('!(.*?)\s+<\s*(.*?)\s*>!', $header, $result);

$formatted = array();
for ($i=0; $i<count($result[0]); $i++) {
  $formatted[] = array(
    'name' => $result[1][$i],
    'email' => $result[2][$i],
  );
}
print_r($formatted);