|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑
5 y1 n5 a% b8 b* K. c# X! p
+ s/ l2 y) {4 I! K- j) K0 }- Y' J! D花了几分钟帮你写了下 % S0 R8 O' @1 O8 ^
例如源文件为这个txt
; n7 ~/ @+ \2 g- W2 U. f➜ ~ cat x.txt1 x6 |1 g/ |" b. {1 w, L
hitest
7 n- J0 l6 k9 D1 v2 D& m& V[email protected] K/ X' o7 m8 n* j3 \
hi [email protected] test
( w" h% O" ^( E0 d4 r[email protected] [email protected]
; L& {% z4 Q4 n/ j9 h执行后,得到result.txt( g! @0 W6 }" u2 j. Q
➜ ~ cat result.txt
# R; [6 O7 n% { [" F5 i[email protected]
! q/ H: v6 {& u[email protected]
( M0 [, [- g4 q" |6 V, ?% R8 y[email protected]# P6 P6 t- s' s% f: F1 L4 k3 L T
[email protected]0 T( D+ \+ U9 S( [
- J# S3 A* u/ Z! O
代码如下:9 G" f6 o z& e& c
- <?php; H$ x/ t) u* t0 M: l
- $file = 'x.txt';
; q: i0 Y0 y* m3 u& l) d - $fp = fopen($file, 'r');
" K: A3 {) v! m+ z" E3 {5 t - if (!$fp) {
. I0 ^5 z$ C2 O* z - exit('failed to open'.$file);
; Q& t( A$ M) B. v5 M- T$ w - }
& K. \* R9 m/ \% B$ C- C$ b - $lineNumber = 0;
$ l+ a9 t ?% k1 ~; ~! Z - while($line = fgets($fp)) {1 K$ n9 O+ v/ r, F0 T, u( \
- $lineNumber++;; U5 i1 t8 p4 a8 d
- $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';1 f1 F/ y, |0 C1 P7 T5 b' Q# V
- preg_match_all($pattern, $line, $matches);2 W* Z8 q& g C& h9 t
- if (!empty($matches[0])) {* i' d3 x) D* C |6 N: T1 {+ l
- foreach ($matches[0] as $mail) {8 P- Y5 L* f" i2 M5 N
- echo $mail.' found from line '.$lineNumber.PHP_EOL;% u( l* I2 P6 Z7 @
- file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);$ T/ M2 D5 c. m- G0 f2 Q
- }) Y% _3 d4 Z* `
- }
) R( t: z$ T0 k0 @6 b* R' p- k - }
6 b6 p* F. n( {' ]' d& r - echo "All Done";
* U \! q0 O" |+ d
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重
/ A5 i( H" f' p使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|