如何用txt文件从PHP中找到一个特殊的单词?

时间:2018-07-22 16:50:50

标签: php search text

我有一个PHP脚本,可以从txt文件中找到一个关键字,但是结果却显示了整行。在这种情况下,我希望结果只显示特定的单词。

这是txt源文件:

Lorem ipsum dolor sit amet aaaaa@xxx.com, consectetur adipiscing bbbbb@xxx.com elit, sed do eiusmod tempor incididunt ut 

labore et dolore magna aliqua cccc@xxx.com. 

Ut enim ad minim veniam ddd@xxx.com, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea jjjj@xxx.com commodo 

consequat. 

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint 

occaecat cupidatat non proident@xxx.com, sunt in culpa qui officia deserunt mollit anim@xxx.com id est laborum.

我使用以下PHP代码:

<?php
$file = 'D:\tes.txt';
$searchfor = 'xxx.com';

// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');

// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
   echo "Found matches:\n";
   echo implode("\n", $matches[0]);
}
else{
   echo "No matches found";
}

?>

使用该代码,结果为:

Found matches:
Lorem ipsum dolor sit amet aaaaa@xxx.com, consectetur adipiscing bbbbb@xxx.com elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua cccc@xxx.com. 
Ut enim ad minim veniam ddd@xxx.com, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea jjjj@xxx.com commodo consequat. 
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident@xxx.com, sunt in culpa qui officia deserunt mollit anim@xxx.com id est laborum.

但是我想要这样:

aaaaa@xxx.com
bbbbb@xxx.com
ddd@xxx.com
cccc@xxx.com
jjjj@xxx.com
.........
.....

需要一些帮助,因为我对编码一无所知,但我需要此脚本。.谢谢

1 个答案:

答案 0 :(得分:1)

您匹配整行,因为您使用.*并锚定^$来声明行的开始和结束。要匹配所有电子邮件地址,您可以将$pattern更新为:

$pattern = "/\S+@xxx\.com\b/m";

您的代码可能如下:

$file = 'D:\tes.txt';
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');

// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
$pattern = "/\S+@xxx\.com/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
    echo "Found matches:\n";
    echo implode("\n", $matches[0]);
}
else{
    echo "No matches found";
}

Demo

不会一次或多次匹配空白字符S+@,后跟xxx.com,最后是word boundary \b < / p>