|
|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑 ) C+ y5 \7 z$ O& ?- \1 `) P& w, o3 y
) U7 ]+ D! F6 d: R) `8 j
花了几分钟帮你写了下
, @5 K- t% N2 z, h" I8 j例如源文件为这个txt: K3 S+ l; _9 j4 F* F
➜ ~ cat x.txt
8 q u+ v$ {6 X& rhitest
" c* p2 S5 D4 D0 \[email protected]6 E6 j7 P2 B1 z6 Y$ q+ q
hi [email protected] test
$ w4 T7 n; L+ T[email protected] [email protected]5 e9 p2 s- w `
执行后,得到result.txt1 l; g7 u1 z/ A9 \* g7 h" R
➜ ~ cat result.txt ) `8 c! G$ W1 U" x# Q
[email protected]
; A* u" n1 u5 j4 P2 b8 [[email protected]
- V/ Q3 x, h+ V6 K[email protected], Z6 N4 W8 _: `1 q# Z; o# u
[email protected]
% Q& B9 K2 S. C0 B% D& l% W+ [& n% ~" z
代码如下:, z5 G8 U' g3 Q( L$ o, R1 O
- <?php
1 F0 {) G5 {$ r9 f) [7 M; f- Z& R+ @ - $file = 'x.txt';3 Q: A5 o8 H b4 o
- $fp = fopen($file, 'r');' z4 m- h* i4 f' i5 n
- if (!$fp) {
1 }# t, y: U' c2 j2 {/ L! d - exit('failed to open'.$file);0 o$ X; k' i* d
- }4 Z1 ] E4 j6 p) f/ {
- $lineNumber = 0;$ j5 K: K( D, T+ }+ s& D
- while($line = fgets($fp)) {
+ f/ w% D0 p1 b - $lineNumber++;
& `" `' ^) e* ?% K/ h, H - $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';, c0 Q1 ?: c& }' e* u
- preg_match_all($pattern, $line, $matches);, [& ]$ @5 H4 _% ^
- if (!empty($matches[0])) {
' e1 b& G! k+ n- H9 q - foreach ($matches[0] as $mail) {! a' O$ Y4 v- }7 v/ g$ p3 _2 V7 M2 ]
- echo $mail.' found from line '.$lineNumber.PHP_EOL;8 e; ~2 ?" I, t5 V2 V
- file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);
- V$ H3 T4 l3 \3 n8 I0 Q# s3 q - }
1 S; b# X, ?3 v - }6 X* |2 @; h5 c" _" @, b* b
- }
+ b) D! h% N9 S+ R8 C4 @ - echo "All Done";2 K0 s; D6 v% O! @1 ~! ?
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重
. E* p3 [$ v) b; I+ o7 d' i使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|