|
|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑
8 e: L* W$ x3 }; a K3 _$ f* \. l0 Z$ D5 r S
花了几分钟帮你写了下
& s- c( u( o0 i9 y例如源文件为这个txt
0 S% I3 j* F& n9 G+ W( ^4 c➜ ~ cat x.txt8 W4 i1 e. u4 ]( o- a
hitest
; ?2 T6 L- S" @' k& p[email protected]
/ @" G5 Y: F8 ]* E) `. @hi [email protected] test
* V; \/ ]3 }6 Z! s, q: k J[email protected] [email protected]4 S L! L. y" P- C/ k- b* a/ U
执行后,得到result.txt
P! g" T$ A' J. Q1 h4 `4 f1 `➜ ~ cat result.txt
$ K, r! z/ v" A' ][email protected]' c) [, K& J- }- p( k
[email protected]. d& e0 I6 P! `: A; P2 w
[email protected]
) x) M+ W0 X! A6 u" r3 Q[email protected]
4 N% U" ?6 f7 M; Q) N/ S% T: r, U
$ V# L+ q2 S2 c' s3 ?/ j代码如下:
# `* q4 N& R$ ~4 h% v0 y- <?php
; W5 }$ y7 k% q% a - $file = 'x.txt';
8 I& a3 k( M; I( I4 J - $fp = fopen($file, 'r');
( Z% q8 q5 `" R - if (!$fp) {* Y1 T/ J& k8 a" X2 C- U# e4 c+ `
- exit('failed to open'.$file);
1 B2 Q# `$ D: m8 K, u6 @5 ~ - }
: P$ u% _ N7 B* x7 k) M - $lineNumber = 0;2 b& U4 W9 V: J% w# l5 d( w) r8 v6 y
- while($line = fgets($fp)) {# {. ~. W- ~ k
- $lineNumber++;
& c7 V/ c' k4 l7 W7 r; H - $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';5 d3 S6 W. ~8 c) I# K) i) Y
- preg_match_all($pattern, $line, $matches);2 U# _2 Y* b: W$ v5 l) d- M2 O0 _
- if (!empty($matches[0])) {1 s$ G: ~2 m+ Y: e: q
- foreach ($matches[0] as $mail) {
# ^7 @1 @: z, _6 F. X1 { - echo $mail.' found from line '.$lineNumber.PHP_EOL;
. S/ c6 K z- a% @2 l - file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);
4 G2 |1 v, f: M, k! I, D) H; o - }7 n& P& [* d# r1 j
- }
6 h3 P! Z9 V& A' d; A, M) P- t - }( F# G) _, U5 B; @. r" V1 Y
- echo "All Done";
5 I/ Y* |; M5 N5 U! N# d
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重0 F$ b- ] u, B
使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|