|
|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑
' w" H0 H! w& E/ }( i6 k
7 q. W( v4 l3 Z+ l8 j; W花了几分钟帮你写了下
G8 d) V$ l2 l例如源文件为这个txt
7 S7 Z$ j' ]* r# {+ U➜ ~ cat x.txt
, K$ P- C7 z1 L1 M" t- ~hitest
0 H$ r4 _' S% L2 o[email protected]
( y: F. ^" N- E$ ]7 l/ ~# Thi [email protected] test7 C6 C5 ^. R% |7 b8 y* `! G
[email protected] [email protected]
$ f( S S& @5 _9 j执行后,得到result.txt0 ~# g) U' i% {) I( W% C
➜ ~ cat result.txt 3 P7 p: [, r/ B9 J6 k2 G1 y& D" S
[email protected]5 G# _4 f) s% O3 \" N5 @, [! J* D
[email protected]. D4 e3 n) _' d. L+ V6 |* U N5 q
[email protected]
1 ?7 I; n4 W. D: S[email protected]
! j$ ^0 s& ^: x8 a; t; d8 E' h
: {3 q' P' V. b l& u$ v0 k" w h# f代码如下:! Z# {% w7 j! L" O4 A6 l
- <?php) ]5 [. A' I& Q/ k7 _7 p
- $file = 'x.txt';
5 x: S4 |9 R9 | - $fp = fopen($file, 'r');
2 t }8 X% c% `! P" ?1 ^& u - if (!$fp) {4 R+ U9 N) Y5 V
- exit('failed to open'.$file);
/ d% S3 X4 b- Y/ }1 c - }! X" S/ x' I: V, c
- $lineNumber = 0;
; U# T! z+ [0 H! p. i& V - while($line = fgets($fp)) {
7 i6 Y1 q2 @, E) E) _5 p% I5 B; k - $lineNumber++;2 i5 r, d$ Y5 n1 ~9 F
- $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';% [- {# G$ r t4 L4 i; J$ H
- preg_match_all($pattern, $line, $matches); B# ^$ U. G0 ?6 i
- if (!empty($matches[0])) {- L- u, H4 k1 Q' g
- foreach ($matches[0] as $mail) {, w$ ^* `5 g4 o X9 \! u$ i
- echo $mail.' found from line '.$lineNumber.PHP_EOL;
* I- M' e+ F. x S! b5 V - file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);
* l. H& O0 m: f2 {) B - }
1 G3 `' \$ _6 T& W - }, c& h/ T1 @5 [" B: c9 m9 [
- }9 d, d6 D& z& \0 h- p6 b5 {- B @. @
- echo "All Done";# |' x; V* K7 H7 H
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重
0 ~ i* r9 W$ D& c9 g$ a使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|