在PHP中,如何从一组文本中提取多个电子邮件地址并将其放入数组? [英] In PHP, how do I extract multiple e-mail addresses from a block of text and put them into an array?
问题描述
$ string = file_get_contents(example.txt); //加载文本文件内容
$ 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); //找到匹配的模式
但是,我得到一个只有一个地址的数组。因此,我猜想我需要以这种方式循环这个过程。我如何做?
你的代码几乎完美,你只需要替换 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
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?
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屋!