|
|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑
4 U/ }7 @. F* F: W7 r6 p. A4 a& q' v$ j ]/ s
花了几分钟帮你写了下
* Y* R8 e/ _( I+ S( Z4 Q例如源文件为这个txt$ \( b% W- v" I2 ]7 ]
➜ ~ cat x.txt. _- b6 j/ _0 V) {; j4 @5 N; G
hitest5 y& S0 l. D3 ]8 c1 m
[email protected]
$ x8 q& P+ V i# ~6 S7 yhi [email protected] test
! j3 `5 K$ {/ j7 E[email protected] [email protected]7 s( m7 Y; X4 Q' w9 C
执行后,得到result.txt
- e5 z2 E( b( ~! Z ?! B9 W➜ ~ cat result.txt
' T+ W! V0 [1 o[email protected]% Y5 j5 B. b, k7 W; k
[email protected]
- M2 t( m5 w+ v[email protected]) d' e: G5 |# z4 _2 C
[email protected]
% _8 g! e, _! Q
2 o( J: k8 A6 s- V% A5 g6 h' m代码如下:
# V% s* C5 `$ L- S/ `- <?php
3 p* D& P, G, F9 _- h - $file = 'x.txt';+ Q& n; W$ p4 f4 O' `5 M$ v
- $fp = fopen($file, 'r');
( P4 T: \0 c- y+ Y$ L8 r$ } - if (!$fp) {1 }# \- M& A" M( S
- exit('failed to open'.$file);
9 O' J3 b, A2 p4 g: \6 S0 b. K - }7 y5 w+ W0 P( [4 m( N% ]* s
- $lineNumber = 0;/ r1 V, \/ w" P9 z1 ?% ^- W7 G
- while($line = fgets($fp)) {
+ \3 C; D) k5 g - $lineNumber++;' W7 [- e9 B9 @4 ?+ a
- $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
1 S& ^. L: s B - preg_match_all($pattern, $line, $matches);
0 k3 N$ F7 H+ Z6 Z0 J; z7 ^7 i - if (!empty($matches[0])) {
8 u1 Q4 k2 `% _, Q& ^0 l - foreach ($matches[0] as $mail) {
1 p0 K/ j6 i3 `2 [9 F; P# L - echo $mail.' found from line '.$lineNumber.PHP_EOL;$ F3 K5 y1 b1 [
- file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);
* r" M9 G/ h' k' B# c: y/ D' m - }3 i* j _$ c0 H9 h
- }
! f- M; R+ k, c; t' o - }
' `+ J' F- W7 `3 b" } - echo "All Done";; L2 H% d4 p9 J& d1 N4 ^& V6 w
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重0 A9 W7 s/ p$ v b
使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|