|
|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑
% B! e) x u; V, Z& r! u7 w6 e" v3 R( P- S. V
花了几分钟帮你写了下
. Y4 D+ R3 p) q, O# |例如源文件为这个txt
( B. h! }7 Z% ?➜ ~ cat x.txt; |8 X0 p+ g* u3 C. f* a9 A
hitest# F5 H) P! p' y- y6 ^
[email protected]8 p4 L0 G1 K4 Y& J$ q, i7 u
hi [email protected] test
8 l( S3 n/ q2 i" c& L6 M[email protected] [email protected]/ c% ?" y( ?( c% r' }- {& j6 N
执行后,得到result.txt* A7 R+ D. f; D5 R" W+ d' {' [' j) N$ |8 s
➜ ~ cat result.txt . W {. g# d; P
[email protected]
7 X1 I* u2 K. {, w2 _[email protected]
0 \8 J' I9 D" @' {( f3 n; Q[email protected]. i7 d0 L7 h# r3 F% K x
[email protected]
E: ` O& t8 d1 O1 w
$ p9 Z1 \6 S* K代码如下:2 @; C7 C8 s1 t
- <?php: s1 Z! i, e9 n& F7 E y) k
- $file = 'x.txt';
: ?! M( z! j+ o - $fp = fopen($file, 'r');
. p) u7 D( m* v: R5 e( q0 G - if (!$fp) {
* M- @! D' j: [8 F( ?6 [ - exit('failed to open'.$file);! k7 M- J& V6 _% A! n) G1 D
- }2 Y3 ]+ o! R+ T* N7 | m3 p
- $lineNumber = 0;( S/ i* a+ m' t
- while($line = fgets($fp)) {
% m8 C* _6 ]2 a. a) F! b - $lineNumber++;, E9 G' h0 S q5 B; Z) \+ T, ^
- $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';( k2 B: j" s& P3 }* H) I
- preg_match_all($pattern, $line, $matches);% P4 P4 m* a) A
- if (!empty($matches[0])) {
Z9 x0 K8 }# u( L# X% b6 i% \ - foreach ($matches[0] as $mail) {7 h# c# j, \! }0 Q
- echo $mail.' found from line '.$lineNumber.PHP_EOL;
& p% O# C. z; H - file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);8 n' P# |: k. M. _; l
- }; q S$ [- U2 x3 x: A5 _9 `
- }
& P8 T$ ]# R# R& Y6 `1 y3 l - }/ v, P5 I! G9 y) `" |+ O
- echo "All Done";
; U2 L% ^% f/ o1 p
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重
; Y# j. k% Z g1 w/ S, s. Z使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|