|
|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑
, V" }( \& m) ~/ }
8 V, i7 J2 q) [7 R花了几分钟帮你写了下
* e4 B% t9 }0 K7 `2 u例如源文件为这个txt
+ J$ I7 o; h5 A8 B. N* ~5 `➜ ~ cat x.txt3 J' u9 E# ^# ]* b5 s" C- s: |, z
hitest
- o1 R9 l2 x; c! c2 [[email protected]1 O. Q% q+ x% l2 k: I8 ~
hi [email protected] test
. P4 N- i) C- P. Z" `2 q[email protected] [email protected]& v+ g i# Y" [
执行后,得到result.txt3 w+ e% [* ?' m. ~
➜ ~ cat result.txt
9 T) X: M8 ]/ w, L- n$ A- j4 Q[email protected]- B5 ^# V, J0 N2 }9 T# x' K
[email protected]
* e4 J; ^+ S+ O! Y[email protected]
. t/ w5 u6 O K3 J: |& d7 `/ F! I/ ?[email protected]
" i$ {/ c0 e3 ~) I7 g: v$ M; }: S7 ^* Z9 r
代码如下:: |3 [+ H9 C7 Y9 y4 H1 @8 J, a
- <?php
3 ]3 k. o5 Y5 u4 \1 J9 y2 d - $file = 'x.txt';
+ M& Y5 C( o. J# ?. [ - $fp = fopen($file, 'r');
{" t5 ^5 f$ Z0 v7 n0 h7 M - if (!$fp) {( h1 ]- k) o6 f3 T9 w
- exit('failed to open'.$file);6 @/ y: H+ J4 [, h8 T$ Y
- }
, {9 r4 P0 ^9 v* F/ i) r5 l( u# L - $lineNumber = 0;
6 Q; a0 T8 T- D; o5 ~. f - while($line = fgets($fp)) {$ h( u) ]4 N: K* ~9 C8 @$ b
- $lineNumber++;2 _6 t+ {# R2 x. a! T
- $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';4 c: {; ~- ~& p& ]8 Y8 G
- preg_match_all($pattern, $line, $matches);' I% W( y* g9 E3 z6 K: U/ b
- if (!empty($matches[0])) {5 v/ ~7 c' [8 [, `
- foreach ($matches[0] as $mail) {
9 I" p9 K w3 U! t - echo $mail.' found from line '.$lineNumber.PHP_EOL;
/ p; C0 T( ?9 t' V' b - file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);8 ^1 d' J5 J6 F
- }. q l! Z: [7 b! _% l# \- E* ~
- }
' g( n7 `& X7 r8 E - }
2 q* @1 W4 G5 N$ H) j - echo "All Done";
: i9 W6 I2 w. E/ Q5 Q& J
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重$ L$ o- n% P1 d, w( J9 m, D( Z. B
使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|