|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑
6 O$ T# m) s1 X3 b2 }
; Z/ B* l( o8 n: {' d" e* ^花了几分钟帮你写了下 0 |! R6 g" X, |7 b/ w5 p; Y% a
例如源文件为这个txt
! s- [* t( j% m g5 _3 ^; P➜ ~ cat x.txt; F% g+ @ l- E0 s; e i
hitest
" a$ |; B( i; c1 x: X" v[email protected]
4 ^/ _$ E1 \8 dhi [email protected] test( Q1 J3 U( @2 b' F
[email protected] [email protected]
) l: g1 n6 o0 H0 B$ D A. y# W# E+ v执行后,得到result.txt# T. k7 V% c7 k& j6 c/ }0 V( T/ s
➜ ~ cat result.txt
6 U4 V, J5 p7 J! A- N8 S[email protected]
9 G/ l3 i* t; n. h# Q* K[email protected]7 X2 I) Z+ ]9 Z% g& a
[email protected]' A& y& N7 I- Q3 C/ N: G
[email protected]5 Z5 L, e* E N' W$ G
" E4 ^6 Y( V4 U, H2 ~) V- ^, b代码如下:4 c, {! P4 n9 [/ M f
- <?php
3 s* o7 m1 ]) X0 `- y0 O, Q4 A) K: R - $file = 'x.txt';% H; U% e) f* Q5 ?! s
- $fp = fopen($file, 'r');0 `) Z0 l! R# t. A/ a1 L2 M0 F2 I
- if (!$fp) {
. q- }- B. _7 @- ^8 t) d# X - exit('failed to open'.$file);" i" s9 F: ~- o- j- s: a6 s% W1 V% I
- }
: |* X1 v8 q y, Y# `# X5 n5 H - $lineNumber = 0;
6 b$ ~1 j# {5 ~0 {% P - while($line = fgets($fp)) {
7 Q: r0 v g6 [% ` - $lineNumber++;5 b& w4 G" K# h7 x6 F- I: D7 f* c( W
- $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
! p* Q6 i; Z: d1 X2 h# ^ - preg_match_all($pattern, $line, $matches);; M8 G Y% c2 k: O
- if (!empty($matches[0])) {$ S& ]; V& i5 ?9 g& F
- foreach ($matches[0] as $mail) {
- ^4 @4 b! w! ]" `3 j m) m - echo $mail.' found from line '.$lineNumber.PHP_EOL;
. i1 J1 C. @; L" W! b - file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);9 G: u9 ?* Y; l3 [: @6 U
- }
7 ^0 y4 A3 _" O" g2 X f - }
6 V! M7 }& n! y9 p+ a" Q - }
; j! m6 Z3 f4 N2 q/ p! u5 O+ c - echo "All Done";* `$ N' o: `6 E# P5 W. {8 e
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重& y; Q6 m/ C( `
使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|