|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑
& b5 @( F6 r$ F: A2 l! ~. U+ O: G
& ]; k8 R+ r- ?- D/ N d. c花了几分钟帮你写了下 . l1 ^3 e' m9 w/ y* W) e
例如源文件为这个txt0 ]$ z1 f4 S2 j+ J; G, S0 Z
➜ ~ cat x.txt
9 z+ [% F0 C' ]9 F( Mhitest
0 d) }9 B; f& [+ @ Z8 O$ ^[email protected]
& R) I6 [3 i" D- O: k: bhi [email protected] test7 s+ r/ e7 N$ \6 |+ e( Q" X) e+ x
[email protected] [email protected]" N( U# B, g! G5 u, }* Y6 G/ P
执行后,得到result.txt
: _: G# P S" N" \" `, C! {5 y➜ ~ cat result.txt
8 n( G7 ~' e; y$ w6 t; P[email protected]
' c. ~7 s# r& a8 v2 Y[email protected] W4 u2 O5 ~4 F
[email protected]
5 t7 \/ `" j0 l. i% T# i1 D[email protected]- g1 v4 J& R8 K8 ~$ r/ w0 j% n+ d
( c9 l# }$ {3 {! i代码如下:) Q g* v/ `/ B. x4 _5 k9 z9 Z
- <?php5 @: q4 ]0 o3 q8 R- a
- $file = 'x.txt';
! j0 }4 D; ?8 C - $fp = fopen($file, 'r');7 K- W4 B1 W# A
- if (!$fp) {
' a+ c, W% w4 [; J7 N - exit('failed to open'.$file);
( R3 W l# B( g. n- W, A - }
, A! K+ C' h( X2 B. I! {: \ - $lineNumber = 0;
- U* M; r+ m3 {9 T- w - while($line = fgets($fp)) {
, r3 l4 q$ O1 @9 @ - $lineNumber++;
+ @' C/ }& H, `8 H, [: m - $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
9 K4 d; F" O. s, C+ y) g - preg_match_all($pattern, $line, $matches);) e" t8 V$ g* z f
- if (!empty($matches[0])) {
, R+ x1 b+ s+ g6 T/ Q* W% V0 ? - foreach ($matches[0] as $mail) {
6 Q2 w) t" G( U# y4 z - echo $mail.' found from line '.$lineNumber.PHP_EOL;6 n) \" B2 R3 I6 w. n- r5 I
- file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);
$ P6 [5 I6 w( Z3 z9 O - }" x3 c/ e% ~$ e2 S6 o) J/ j" F6 ^5 `
- }% e# s3 Y, c" b* R" O l
- }
) m. z' E c3 J/ Z1 J - echo "All Done";% ^& _& T* Y1 Y/ I4 [
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重
/ s5 x3 G0 E( w% b+ P* D使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|