php - 如何从 txt 文件中用 PHP 找到一个特定的词?
问题描述
我有一个 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
.........
.....
需要一些帮助,因为我不知道编码,但我需要这个脚本..谢谢
解决方案
您匹配整行,因为您正在使用.*
和锚点^
并$
断言行的开头和结尾。要匹配您可以更新为的所有电子邮件地址$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";
}
这将不匹配空白字符一次或多次S+
,并在后面加上一个单词边界并在结尾处@
签名xxx.com
\b
推荐阅读
- python - 通过 Python 中的 SOAP 请求合并 Salesforce 中的重复帐户
- python-3.x - 我的 python 左旋转数组代码在 IDE 中运行良好,而不是在 HackerRank 平台上
- javascript - Nodemailer 不发送邮件文本
- powerbi - Azure 数据资源管理器 - Power BI。从外部表读取失败
- css - 我正在导出 CSS 值变量,为什么没有应用它们?
- javascript - 当它是一个功能组件时如何获取孩子的参考
- angular - 剑道角度多模态叠加
- java - 切换到下一个活动会引发错误 ro.vendor.df.effect.conflict
- moodle - 如何更改“exams2moodle”问题的 R 背景颜色?
- sql - 选择失败。3707: 语法错误,在 ')' 和 ')' 之间应该有一个 'END' 关键字