|
|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑 . ^* H9 N7 p8 u1 g. r* N& X- B1 e2 E
) ^6 G5 E' z& r/ ~. \1 K6 ~
花了几分钟帮你写了下
" U, \/ h& y' M4 E* l3 _例如源文件为这个txt4 B/ k( Y) ~2 n9 b: ?5 @5 B; u
➜ ~ cat x.txt# U7 ~4 s4 ?9 [1 D& ]
hitest9 L' o, ^( G+ \+ P# S7 n* c( r4 B' @5 [
[email protected]7 B2 O& D0 j, A, m
hi [email protected] test1 [% G' e) F. T$ Y
[email protected] [email protected]
! k; {1 R, L( [$ F9 s执行后,得到result.txt+ H7 h# w" K2 v9 v$ {+ f) c
➜ ~ cat result.txt
$ [! j+ J; s+ n* c2 y N5 j, U4 m[email protected]# Y. ] f5 n2 _' R' B
[email protected]8 s; k- }/ S5 b6 M c y( X
[email protected]! e3 i2 s1 Q6 c3 L
[email protected]
]- k1 ^4 r! Q+ O* s; J: w; f, w G3 j! N4 M% S' R, d* ^
代码如下:
, E( a0 I- e8 x- <?php6 r/ d/ N$ J" S. \$ n9 F3 Y$ r/ n
- $file = 'x.txt';
0 K& J! ~( Q2 V- K! I; T- z - $fp = fopen($file, 'r');- h1 C$ Z2 u. g$ y
- if (!$fp) {
5 B6 Z9 l, P4 M' i) g/ }! Y; s - exit('failed to open'.$file);
8 \/ j( C2 `8 g6 b9 g. i5 u - }
: d' P4 K$ K- T2 V; d - $lineNumber = 0;# R' I5 m _1 J, N# j9 r
- while($line = fgets($fp)) {1 O. ~8 x9 \; {0 Q, a5 e
- $lineNumber++;
. w! ?! I7 K) m. @* S) d' j - $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
* g1 U" T" [. A1 D/ X - preg_match_all($pattern, $line, $matches);
a8 J9 a7 N) F; p; f - if (!empty($matches[0])) {. b9 A: E1 {" j' B! t2 Y
- foreach ($matches[0] as $mail) {
# @6 E: s" n, @# x3 z; T. H6 P/ l - echo $mail.' found from line '.$lineNumber.PHP_EOL;& [3 i% d8 J0 H( y; q9 h2 c% r
- file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);- r- y1 K, d) D9 ]+ h7 s
- }
) L+ L1 H' ]/ Z# s2 Q* q - }
& l2 g$ B, r- @ - }
8 e+ n5 S9 o$ R% }' R3 g - echo "All Done";
( {" ?* Z, P5 G1 ?' N4 Y
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重7 a% t( J5 E# p: v( B1 P5 d: @
使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|