在 PHP 中,如何从文本块中提取多个电子邮件地址并将它们放入数组中? [英] In PHP, how do I extract multiple e-mail addresses from a block of text and put them into an array?

查看:21
本文介绍了在 PHP 中,如何从文本块中提取多个电子邮件地址并将它们放入数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本块,我想从中提取有效的电子邮件地址并将它们放入一个数组中.到目前为止,我已经...

I have a block of text from which I want to extract the valid e-mail addresses and put them into an array. So far I have...

   $string = file_get_contents("example.txt"); // Load text file contents
   $matches = array(); //create array
   $pattern = '/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+.([A-Za-z0-9_-][A-Za-z0-9_]+)/'; //regex for pattern of e-mail address
   preg_match($pattern, $string, $matches); //find matching pattern

然而,我得到一个只有一个地址的数组.因此,我猜我需要以某种方式循环这个过程.我该怎么做?

However, I am getting an array with only one address. Therefore, I am guessing I need to cycle through this process somehow. How do I do that?

推荐答案

你的代码几乎完美,你只需要将 preg_match(...) 替换为 preg_match_all(...)

Your code is almost perfect, you just need to replace preg_match(...) with preg_match_all(...)

http://www.php.net/manual/en/function.preg-match.php

http://www.php.net/manual/en/function.preg-match-all.php

这篇关于在 PHP 中,如何从文本块中提取多个电子邮件地址并将它们放入数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆