Php 具有相同模式的输出值
我用php编写了以下代码:Php 具有相同模式的输出值,php,arrays,output,Php,Arrays,Output,我用php编写了以下代码: <?php $mails_list = "user1@example.com, User 2 <user2@example.com>, User 3 (user3@example.com)"; $mails_arr = explode(',', $mails_list); foreach($mails_arr as $mail){ echo "{$mail}<br>"; } ?> 如
<?php
$mails_list = "user1@example.com, User 2 <user2@example.com>, User 3 (user3@example.com)";
$mails_arr = explode(',', $mails_list);
foreach($mails_arr as $mail){
echo "{$mail}<br>";
}
?>
如何使用PHP实现这一点?您可以使用正则表达式捕获所有电子邮件:
preg_match_all('/[<(]?(\w+@\w+\.\w+)[>)]?/', $mails_list, $matches);
echo implode("\n", $matches[1]);
preg_match_all('/[)]?/',$mails_list,$matches);
回音内爆(“\n”,$matches[1]);
您可以使用正则表达式捕获所有电子邮件:
preg_match_all('/[<(]?(\w+@\w+\.\w+)[>)]?/', $mails_list, $matches);
echo implode("\n", $matches[1]);
preg_match_all('/[)]?/',$mails_list,$matches);
回音内爆(“\n”,$matches[1]);
给你
<?php
$mails_list = "user1@example.com, User 2 <user2@example.com>, User 3 (user3@example.com)";
$mails_arr = explode(',', $mails_list);
preg_match_all('/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/i', $mails_list, $found_mails);
foreach ($found_mails[0] as $each) { echo $each.'<br>'; }
给你
<?php
$mails_list = "user1@example.com, User 2 <user2@example.com>, User 3 (user3@example.com)";
$mails_arr = explode(',', $mails_list);
preg_match_all('/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/i', $mails_list, $found_mails);
foreach ($found_mails[0] as $each) { echo $each.'<br>'; }
@PSantos:很高兴能帮上忙。请注意,如果电子邮件地址中包含字母、数字和\uuuu
以外的字符,则此操作不起作用。不过修复很简单,只需将所有出现的\w+
更改为[\w.+]+
(用电子邮件组件可能包含的任何字符替换
。@PSantos:很高兴能提供帮助。请注意,如果电子邮件地址中包含字母、数字和\uuuu
以外的字符,则此操作不起作用。不过修复很简单,只需将所有出现的\w+
更改为[\w.+]+
(用电子邮件组件可能包含的任何字符替换
)。