如何在PHP中的电子邮件地址后面的分隔符后面收集数据


How to collected data after a delimiter, following an email address in PHP?

我正在编写一个PHP脚本作为一种学习项目。完成后,它将允许用户将一段文本粘贴到HTML表单中,并从文本中提取所有电子邮件地址和人名,并将其显示在列表中。

到目前为止,我可以使用以下方法提取电子邮件地址:

$pre = htmlspecialchars($_POST['pre']);     
//Regex to find email addresses
$email_pattern = '/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+'.[A-Za-z]{2,4}/i';    
//save email addresses in an array
preg_match_all($email_pattern, $pre, $email_matches); 
echo "<h1>Mailing list</h1>
<p>Here are the email addresses contained within the text and the recipients name</p>";
//display list showing each unique email address only once.    
foreach(array_unique($email_matches[0]) as $email) {    
echo $email."<br />";
}

在我使用的特定文本中,电子邮件地址后面总是逗号,然后是"然后是人名",例如:

'example@example.com','约翰·史密斯','example2@example2.com","简·史密斯"。。。等等

我的问题是,对于每个电子邮件地址,我如何将人名理想地保存在一个单独的数组中?

有没有一个正则表达式可以识别电子邮件地址,跳过一定数量的字符,然后选择所有内容,直到检测到另一个字符?

谢谢!

Regex:

$re = "/([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+''.[A-Za-z]{2,4})''''','''([A-Za-z''s]*)'''/"; 

字符串:

$str = "'example@example.com','john smith','example2@example2.com','Jane smith','example@example.com','john smith','example2@example2.com','Jane smith','example@example.com','john smith','example2@example2.com','Jane smith'"; 

查找所有匹配项:

preg_match_all($re, $str, $matches);

$matches将是一个数组:

Array
(
    [0] => Array
        (
            [0] => example@example.com','john smith'
            [1] => example2@example2.com','Jane smith'
            [2] => example@example.com','john smith'
            [3] => example2@example2.com','Jane smith'
            [4] => example@example.com','john smith'
            [5] => example2@example2.com','Jane smith'
        )
    [1] => Array
        (
            [0] => example@example.com
            [1] => example2@example2.com
            [2] => example@example.com
            [3] => example2@example2.com
            [4] => example@example.com
            [5] => example2@example2.com
        )
    [2] => Array
        (
            [0] => john smith
            [1] => Jane smith
            [2] => john smith
            [3] => Jane smith
            [4] => john smith
            [5] => Jane smith
        )
)

这意味着$match[1][n]将是$match[2][n]的电子邮件地址。