Reputation: 123
I do know there are multiple other answers to this, but they do not fit on the code I have. Since the code I have has to handle 1000 lines + per search.
I want to combine the strings of 2 inputs so the PHP script searches for both in the txt files and combines them when outputting.
This is what I tried:
$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
}
When I run this code with both search and search2 filled in: I get this output:
1
1
1
1
1
1
And those 1's seem to be infinite. I hope anyone has a solution.
The outputs should be for both strings: search = 'new'
He is a new player
Gambling is a new sport
New is better than old
search2 = 'website'
It is a nice website
The website is down
The FIFA website is being built
The right output should be:
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
Thank you for reading.
~Conner
Upvotes: 3
Views: 1766
Reputation: 24276
You can try something like this:
foreach (new SplFileObject($logfile) as $lineNumber => $lineContent) {
if(strpos($lineContent, $search) !== false
|| strpos($lineContent, $search2) !== false
) {
echo $lineContent . '<br />';
}
}
Upvotes: 1
Reputation: 861
<?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>";
}
?>
Please see if this code works.
This will give you the lines where $search1
or $search2
appear.
Thank you :)
Upvotes: 3