在包含 PHP 的文本文件中查找字符串
Find string in a text file with PHP
我有一个名为 peoplelist.txt
的文件,其中包含以下文本:
1,mikey.mcgurk,1,boss.man
但是,当我稍后在我的脚本中搜索第二个文件时尝试使用 $name
时,它不起作用:(
people.txt
的内容就是mikey.mcgurk
所以下面的效果很好但是如果我使用$searchfor = $name
,它会失败。为什么?
$handle = fopen("peoplelist.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$parts = explode(',',$line);
$name = $parts[1] . PHP_EOL;
$name = strtolower($name);
$name = str_replace(' ', '.', $name);
$boss = $parts[3] . PHP_EOL;
}
fclose($handle);
}
echo $name; // this echoes out 'mikey.mcgurk', as expected
$file = 'people.txt';
$searchfor = 'mikey.mcgurk';
// 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";
}
注意:这不是 2 个单独的脚本,它是一个 PHP 文件 - 我只是为了这个问题将它拆分 :-)
我认为问题出在正则表达式模式上。但是由于还有其他一些事情可以做得更好,所以我也将其添加。
$list = file_get_contents("peoplelist.txt");
list($something, $name, $somethingelse, $boss) = explode(',',$line);
$name = str_replace(' ', '.', strtolower($name));
echo $name;
$file = 'people.txt';
$contents = file_get_contents($file);
$pattern = "/" . $name . "/m";
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
echo implode("\n", $matches[0]);
}
else{
echo "No matches found";
}
你的正则表达式没有意义。你想让它从任何东西开始。
那为什么要强制入手呢?当然还有文字 $.
我有一个名为 peoplelist.txt
的文件,其中包含以下文本:
1,mikey.mcgurk,1,boss.man
但是,当我稍后在我的脚本中搜索第二个文件时尝试使用 $name
时,它不起作用:(
people.txt
的内容就是mikey.mcgurk
所以下面的效果很好但是如果我使用$searchfor = $name
,它会失败。为什么?
$handle = fopen("peoplelist.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$parts = explode(',',$line);
$name = $parts[1] . PHP_EOL;
$name = strtolower($name);
$name = str_replace(' ', '.', $name);
$boss = $parts[3] . PHP_EOL;
}
fclose($handle);
}
echo $name; // this echoes out 'mikey.mcgurk', as expected
$file = 'people.txt';
$searchfor = 'mikey.mcgurk';
// 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";
}
注意:这不是 2 个单独的脚本,它是一个 PHP 文件 - 我只是为了这个问题将它拆分 :-)
我认为问题出在正则表达式模式上。但是由于还有其他一些事情可以做得更好,所以我也将其添加。
$list = file_get_contents("peoplelist.txt");
list($something, $name, $somethingelse, $boss) = explode(',',$line);
$name = str_replace(' ', '.', strtolower($name));
echo $name;
$file = 'people.txt';
$contents = file_get_contents($file);
$pattern = "/" . $name . "/m";
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
echo implode("\n", $matches[0]);
}
else{
echo "No matches found";
}
你的正则表达式没有意义。你想让它从任何东西开始。
那为什么要强制入手呢?当然还有文字 $.