gpt4 book ai didi

php - 在一个 txt 文件中搜索多个字符串

转载 作者:可可西里 更新时间:2023-11-01 00:59:19 26 4
gpt4 key购买 nike

我知道对此有多个其他答案,但它们不适合我的代码。由于我的代码每次搜索必须处理 1000 行以上。

我想合并 2 个输入的字符串,以便 PHP 脚本在 txt 文件中搜索这两个字符串并在输出时合并它们。

这是我尝试过的:

$search = $_GET["search"]; 
$search2 = $_GET['search2'];
$logfile = $_GET['logfile'];


// Read from file
$file = fopen($logfile, "r");?>
<head><title>Searching: <?php echo $search ?></title></head>
<?php
while( ($line = fgets($file) && $line2 = fgets($file))!= false)
{ if (stristr($line, $search)) { } if (stristr($line2, $search2)) { }

?><font face='Arial'> <?php $lines = $line + line2; ?><?php echo $lines ?></font><hr><p><?php
}

当我运行此代码并同时填充 search 和 search2 时: 我得到以下输出:

1
1
1
1
1
1

那些 1 似乎是无限的。我希望任何人都有解决方案。

输出应该是两个字符串:搜索 = '新'

He is a new player
Gambling is a new sport
New is better than old

search2 = '网站'

It is a nice website
The website is down
The FIFA website is being built

正确的输出应该是:

He is a new player
It is a nice website
Gambling is a new sport
The FIFA website is being built
New is better than old

感谢阅读。

~康纳

最佳答案

<?php 
$search1 = "value 1";
$search2 = "value 2";

$lines = file('my_file.txt');

foreach($lines as $line)
{
if(stristr($line,$search1) || stristr($line,$search2))

echo " $line <br>";
}
?>

请查看此代码是否有效。这将为您提供 $search1 $search2 出现的行。谢谢:)

关于php - 在一个 txt 文件中搜索多个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31936708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com