|
|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑
$ U6 }) Q$ i$ w+ o
3 U$ k/ h; j2 n* Q6 x花了几分钟帮你写了下 5 `8 q1 B% I- h
例如源文件为这个txt
/ P- S1 a/ U M) p- a' A2 X- L➜ ~ cat x.txt {1 o" e" h- N( L, u
hitest
- U. P3 W# V, E[email protected]
/ L1 q. [5 F% }& V! ]$ n4 _hi [email protected] test- y0 k! J& p- ]' a1 ?3 P' f
[email protected] [email protected]* K: Z" M: D# o
执行后,得到result.txt
: T* k2 v8 Z/ g( K5 D- r➜ ~ cat result.txt ) t) r; p' G/ V6 e4 `! K+ q+ t
[email protected]1 J7 _6 y* z1 T' |! ^1 D
[email protected]
# t4 T$ m8 c. i2 X! T; s, `- }[email protected]8 P) @/ i# v: j8 E# M
[email protected]0 I$ i; h8 E+ u4 q0 |- j
]2 w4 t+ M, J0 @* x7 f8 G3 e9 q代码如下:
0 I% O1 F R/ g4 ]) B- <?php% h* F) e( n4 u9 S- M; x0 u- T
- $file = 'x.txt';
$ J. B( @4 {" l, l* V, C) B - $fp = fopen($file, 'r');
1 L: ?# Z+ L, _/ a4 u - if (!$fp) {7 [: P1 R, G# q2 e. H) M
- exit('failed to open'.$file);
6 b( F) i3 g& G6 j - }% Q) W, n+ j W6 a
- $lineNumber = 0;% C q9 w3 f1 t
- while($line = fgets($fp)) {
/ A5 o, r' W6 X1 `, {$ W - $lineNumber++;
2 }5 H. i- H* M6 b - $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
9 `) D6 _0 s+ g0 _ - preg_match_all($pattern, $line, $matches);
5 K0 W- Q0 ]' B' W' Q - if (!empty($matches[0])) {
9 ~- L! r/ _! Z6 A( |/ H - foreach ($matches[0] as $mail) {
5 K6 S' ^' \2 W - echo $mail.' found from line '.$lineNumber.PHP_EOL;
" W, q0 e3 v' t& Z$ y+ ]; ] - file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);
, D3 N! D0 j& O0 L D6 d - }
+ \* d q$ E7 ^* B$ d - }
) f5 g* }! e- g/ b. G$ m; P - }
- @9 C- g+ }4 [) O - echo "All Done";# I, Z$ W) K2 a" H. Q2 O
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重" ?; Q, v; o9 N9 N) n: @3 d
使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|