我知道这方面还有很多其他的答案,但它们不适合我的代码。因为我的代码必须处理1000行+每次搜索。
我希望将两个输入的字符串组合起来,以便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而这一点似乎是无限的。我希望任何人都有解决办法。
输出应为两个字符串: search = 'new‘
He is a new player
Gambling is a new sport
New is better than oldsearch2 =“网站”
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感谢您的阅读。
康纳
发布于 2015-08-11 08:40:33
<?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的行。谢谢您:)
发布于 2015-08-11 08:47:34
你可以试试这样的东西:
foreach (new SplFileObject($logfile) as $lineNumber => $lineContent) {
if(strpos($lineContent, $search) !== false
|| strpos($lineContent, $search2) !== false
) {
echo $lineContent . '<br />';
}
}https://stackoverflow.com/questions/31936708
复制相似问题