|
|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑
+ {4 ?( _) j6 { o& w& ?- p A4 b* {( Y
花了几分钟帮你写了下
% |+ F; _1 d, H/ _& Y6 b例如源文件为这个txt
$ J% G$ `: Z: H5 Y- n, z5 L2 v➜ ~ cat x.txt% y0 M+ H6 H0 ~/ m
hitest
, A$ s: _- [3 e; B# t6 |3 V# V, V[email protected]
1 r. D+ g7 C0 whi [email protected] test
. R u( [3 e! a[email protected] [email protected]; W6 D) q# b6 m7 z0 `8 D& r/ f* u/ u
执行后,得到result.txt( C2 [4 x$ o5 m* r: y1 M
➜ ~ cat result.txt
/ {5 s, {0 M$ f5 d8 L' S" `' ~[email protected]
5 }* b8 V- \/ d( ~$ p, T9 {[email protected]- z8 T: x A$ M- t9 F
[email protected]3 _. `4 b( w0 G5 C
[email protected]+ J( k" M9 e+ ^: ^8 |0 e/ c* c
: G4 g8 P [, s+ K8 L- c
代码如下:# s8 M, Z# p% o
- <?php
# Y% X' m' _- \; ~' w; u! @ - $file = 'x.txt';8 G' O: o, E3 G& i! U
- $fp = fopen($file, 'r');
* f# A2 U0 E+ G! R - if (!$fp) {
5 q4 d5 o' O7 @7 z: P& {1 w7 H - exit('failed to open'.$file);
( _% v- D1 P3 h$ w' |3 q2 a% x - }
( O; o1 `( D a& Z3 N- c- E7 x/ \ - $lineNumber = 0;" r' l- G) a" A- ]
- while($line = fgets($fp)) {: ]6 R* K6 f8 v" m: \
- $lineNumber++;
& b$ B* O# t8 I& [. V- Q - $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
; l% u7 S& ^1 W4 d - preg_match_all($pattern, $line, $matches);
5 e6 u( t: O! n K0 { - if (!empty($matches[0])) {, U* T. H# o5 F. w) h8 @5 ]+ \& ~& `
- foreach ($matches[0] as $mail) {
5 n- i( z# Y2 F. A - echo $mail.' found from line '.$lineNumber.PHP_EOL;9 H7 Q& Z8 ^$ y" ]% Y
- file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);
% ~. S) K, v- Z- \2 Q8 G6 |, [ - }
0 e9 n+ i& W1 h* Y* _9 D0 v - }
7 E- k% G/ |5 r. ^( D# S- V - }
: S1 t7 n* b3 {' d/ g - echo "All Done";3 R& t' y9 ?- \2 M+ R1 {, n
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重4 ]2 {7 s3 L4 v2 l
使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|