|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑
q: d3 j# H' }- ?% e A* s; l$ S# l7 B, h) w3 [+ j. m" |: V K
花了几分钟帮你写了下
3 m; j1 B/ O, w, d例如源文件为这个txt: Z! j3 ~4 a; C5 w2 P4 H
➜ ~ cat x.txt4 {6 ]) f) @! g: u, u! T' r
hitest2 x0 D7 g" F0 e) ~; e
[email protected]
, {" F$ a- D* q* e7 Y3 O( Ehi [email protected] test
( ^/ c+ z1 p$ ^' Q[email protected] [email protected]9 `* O6 }2 j/ O# D z
执行后,得到result.txt
( f) G% y+ [' z# v$ n. ?4 t➜ ~ cat result.txt d; K5 _4 H* f7 F# S- r
[email protected]
! |7 k# w, K& v; m. S[email protected]+ w7 r( i* p' U0 k) ~3 W
[email protected]- L2 u- f( w, c/ x& ~$ y" P0 |
[email protected]
2 G s# m7 k6 Z
) q# {; {9 G# D+ ]代码如下:
; g' h7 m+ A' R$ N' v- <?php
# B# I) h0 L: H! S5 _8 j - $file = 'x.txt';2 N6 J0 X( h" b4 |# S
- $fp = fopen($file, 'r');$ Z/ x2 i% C6 G0 m) e
- if (!$fp) {
( n+ g+ N0 v5 n; R% e6 o - exit('failed to open'.$file);1 a) ?; A7 H( P- D1 R
- }
3 o* u- |; m$ L$ i4 g7 } - $lineNumber = 0;
% p2 e1 W0 k9 N+ r - while($line = fgets($fp)) {
, {. j9 n/ c5 o: T8 _ } - $lineNumber++;+ h+ D6 Y4 A' R. I
- $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
" Z2 i8 W3 x, x1 _ - preg_match_all($pattern, $line, $matches);
- f1 b. Q2 b* Z; U& M6 L4 | - if (!empty($matches[0])) {; h6 i5 o8 ~4 c
- foreach ($matches[0] as $mail) {
! M/ p, D& q, o% j* ]. D - echo $mail.' found from line '.$lineNumber.PHP_EOL;' \5 s, C( ^1 y! R- J' ?
- file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);
2 s* M/ }0 {' {; B) S - }0 K! t$ [, T5 G* v
- }7 U/ K* ^, L9 E" ]' Y/ k
- }
* p' I) k, J0 i6 m: C' O6 Z5 S - echo "All Done";
. D0 \) P7 P& v0 u
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重: V7 \# Z6 z# U% f' d' Q0 a
使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|