|
|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑
( k R* g+ v2 Z7 N& h1 B- h1 s# W9 ?( {
花了几分钟帮你写了下 . g" M% L% p$ B+ k
例如源文件为这个txt
( T0 q( U3 g$ j( Y- L& M$ Z➜ ~ cat x.txt* O' F* `5 C& h0 u" b
hitest
@# J; W8 B9 p: ]$ k$ V[email protected]( X- F7 C: ?$ K+ S$ }5 n
hi [email protected] test
. U% J; s/ w6 d1 u# H0 E[email protected] [email protected]
, ?( b( R/ d9 |执行后,得到result.txt
1 r" Z9 G `* J0 _4 P+ u4 M➜ ~ cat result.txt
- L/ N( [) d0 Y, ~% z[email protected]
1 C7 y& F- H4 W+ Z U) p[email protected]; d' { d0 X+ p, b. b3 y. M
[email protected]
0 i' D$ y3 q% ?; k[email protected]
0 \5 t' P/ w) a9 W3 _$ ^5 h
2 l, U$ i, A4 E代码如下:
( r0 f/ E0 V* b. u5 t9 X" N- <?php
7 k, V0 Y; ~' T8 y8 R! M - $file = 'x.txt';; C! U/ r/ m4 T& r- E
- $fp = fopen($file, 'r');1 E6 U6 {8 p z. H L
- if (!$fp) {$ y9 k# V! {8 G0 D
- exit('failed to open'.$file);
: H/ I: Q9 n$ P3 U$ r' A - }
1 q2 ?" A f1 R3 |# V - $lineNumber = 0;
9 l2 K2 [+ V* B1 x4 B6 a - while($line = fgets($fp)) {
$ {' J2 ~, ?: y% v! K I - $lineNumber++;' y, S! L8 Y2 R# f6 d
- $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
7 W4 N5 c/ q" Z. J6 m - preg_match_all($pattern, $line, $matches);
3 C$ Z. O* o# K/ _5 o" S* b) b9 A - if (!empty($matches[0])) {; q5 |( q8 ^" Z5 ]. D9 f
- foreach ($matches[0] as $mail) {
2 C: j+ J. @) ]+ U - echo $mail.' found from line '.$lineNumber.PHP_EOL;
9 ?1 n* X8 p. q - file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);; w# W! n- @& b$ ]1 u
- }
; P) U; K8 H! s* ]4 E: P9 p - }( j+ b3 V# e7 n; _8 n) @
- }
9 o& F$ `- I% P: x - echo "All Done";$ W2 a3 s0 U4 h+ _4 o
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重
7 w8 y. z) V( [) }使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|