Php 在文本文件中搜索关键字
我正在开发一个PHP搜索脚本,它扫描网站上的一个文本文件存储库中的包,并返回每个匹配的URL。文本文件如下所示:Php 在文本文件中搜索关键字,php,search,Php,Search,我正在开发一个PHP搜索脚本,它扫描网站上的一个文本文件存储库中的包,并返回每个匹配的URL。文本文件如下所示: PACKAGE NAME: package_one PACKAGE LOCATION: ./package_one PACKAGE VERSION: 0.1.2 PACKAGE NAME: package_two PACKAGE LOCATION: ./package_two PACKAGE VERSION: 0.2.1 PACKAGE NAME: package_three P
PACKAGE NAME: package_one
PACKAGE LOCATION: ./package_one
PACKAGE VERSION: 0.1.2
PACKAGE NAME: package_two
PACKAGE LOCATION: ./package_two
PACKAGE VERSION: 0.2.1
PACKAGE NAME: package_three
PACKAGE LOCATION: ./package_three
PACKAGE VERSION: 1.3.4
PACKAGE NAME: package_one
PACKAGE LOCATION: ./package_one
PACKAGE VERSION: 0.1.2
<?
$search = $_POST['search']; //Passed from a user input form
$data_file = file('http://www.example.com/data_file.txt'); //The text file repository
$final_concatenated_results = "";
for ($i = 0, $found = FALSE; isset($data_file[$i]); $i++) {
if (trim($data_file[$i]) === $search) {
$cleanse = trim($data_file[++$i]);
$output_part_one = strstr($cleanse, '.');
$output_part_two = "http://www.example.com/packages_repository/".$output_part_one;
$add_this = "<a href='$output_part_two'>$output_part_two</a>"."<br/>";
$found = TRUE;
$final_concatenated_results.=$add_this;
} }
if ($found == TRUE) {
echo $final_results;
}
if ($found == FALSE) {
echo "Error, package not found.";
}
?>
我的搜索脚本当前看起来如下所示:
PACKAGE NAME: package_one
PACKAGE LOCATION: ./package_one
PACKAGE VERSION: 0.1.2
PACKAGE NAME: package_two
PACKAGE LOCATION: ./package_two
PACKAGE VERSION: 0.2.1
PACKAGE NAME: package_three
PACKAGE LOCATION: ./package_three
PACKAGE VERSION: 1.3.4
PACKAGE NAME: package_one
PACKAGE LOCATION: ./package_one
PACKAGE VERSION: 0.1.2
<?
$search = $_POST['search']; //Passed from a user input form
$data_file = file('http://www.example.com/data_file.txt'); //The text file repository
$final_concatenated_results = "";
for ($i = 0, $found = FALSE; isset($data_file[$i]); $i++) {
if (trim($data_file[$i]) === $search) {
$cleanse = trim($data_file[++$i]);
$output_part_one = strstr($cleanse, '.');
$output_part_two = "http://www.example.com/packages_repository/".$output_part_one;
$add_this = "<a href='$output_part_two'>$output_part_two</a>"."<br/>";
$found = TRUE;
$final_concatenated_results.=$add_this;
} }
if ($found == TRUE) {
echo $final_results;
}
if ($found == FALSE) {
echo "Error, package not found.";
}
?>
例如,如果“PACKAGE NAME:PACKAGE_two”是逐字输入的,那么这将是输出——效果很好
理想情况下,我想知道如何更改代码,这样就不用键入“PACKAGE NAME:PACKAGE_two”,只需键入“PACKAGE_two”或类似“two”或“tw”之类的内容即可。这需要多少调整
此外,我还创建了一个变量,将所有结果URL连接到一个字符串,因此如果搜索“package_one”,它将返回两个URL。。。我认为我的代码做得不对,因为它只生成一个URL
**我应该提到,在匹配包名之后,它会立即找到“package LOCATION:”后面的下一个字符串来获取URL
欢迎您的任何意见,谢谢 看看strpos()函数:
if(strpos(trim($data_file[$i],$search) != false)
它检查您的搜索字符串(指针)是否在草堆(当前输入行)内。
在这里,您正在寻找精确匹配:
if (trim($data_file[$i]) === $search) {
您可以使用查看您的搜索字符串是否显示在以下行中:
if (stripos($data_file[$i], $search) !== FALSE) {
请注意,您需要
==代码>比较,因为stripos
可以计算为0。我知道你要去哪里。如果用户键入“package_tw”。。。有没有办法把这看作是一场“套餐二”比赛?还有,我会更容易/可能爆炸吗;整个文件,因此每一行都是数组索引,然后我可以执行stripos()操作;搜索并将一个添加到该行的索引中,然后使用preg_replace();要从输出中删除“位置:”零件?谢谢你,杰伦@用户1062058package\u tw
和上面的代码将匹配package\u two
,package\u two
,等等。关于其他问题,它们似乎与搜索部分无关,所以也许你最好在另一个问题中问它,或者在原始问题中指定你正在寻找的确切输出。好的,谢谢你,杰伦。我注意到我的问题目前在谷歌上排名第三,当你搜索这个话题时。。。你的回答对别人会有很大帮助。祝你有愉快的一天。谢谢你的回答,我正在调查strpos();现在!对于用户输入,另一个答案中的stripos()似乎是更好的解决方案,因为它不区分大小写。