PHP search text file line by line for two strings then output line
Solution 1
Assuming the values you're searching for are separated by a space, and they will both always be present, explode
should do the trick:
$search = explode(' ', $_REQUEST["search"]); // change ' ' to ',' if you separate the search terms with a comma, etc.
// Read from file
$lines = file('archive.txt');
foreach($lines as $line)
{
// Check if the line contains the string we're looking for, and print if it does
if(strpos($line, $search[0]) !== false && strpos($line, $search[1] !== false)) {
echo"<html><title>SEARCH RESULTS FOR: $search</title><font face='Arial'> $line <hr>";
}
}
I'll leave it up to you to add some validation to make sure there are always two elements in the $search
array, etc.
Solution 2
I also corrected the HTML code. The script looks for two values, $search
and $search2
. It is using stristr(). For the case-sensitive version of stristr, refer to strstr(). The script will return all lines containing both $search
and $search2
.
<?php
$search = $_REQUEST["search"];
$search2 = $_REQUEST['search2'];
// Read from file
$lines = file('archive.txt');
echo"<html><head><title>SEARCH RESULTS FOR: $search</title></head><body>";
foreach($lines as $line)
{
// Check if the line contains the string we're looking for, and print if it does
if(stristr($line,$search) && stristr($line,$search2)) // case insensitive
echo "<font face='Arial'> $line </font><hr>";
}
?>
</body></html>
Solution 3
Just search for your other value also and use && to check for both.
<?php
$search1 = $_REQUEST["search1"];
$search2 = $_REQUEST["search2"];
// Read from file
$lines = file('archive.txt');
foreach($lines as $line)
{
// Check if the line contains the string we're looking for, and print if it does
if(strpos($line, $search1) !== false && strpos($line, $search2) !== false)
echo"<html><title>SEARCH RESULTS FOR: $search1 and $search2</title><font face='Arial'> $line <hr>";
}
?>
Martyn
Interested in all things technology. A self confessed Apple fan.
Updated on November 23, 2022Comments
-
Martyn over 1 year
I am trying to search a text file for two values on a line. If both values are present I need to output the entire line. The values I am searching for may not be next to each other which is where I am getting stuck. I have the following code which works well but only for one search value:
<?php $search = $_REQUEST["search"]; // Read from file $lines = file('archive.txt'); foreach($lines as $line) { // Check if the line contains the string we're looking for, and print if it does if(strpos($line, $search) !== false) echo"<html><title>SEARCH RESULTS FOR: $search</title><font face='Arial'> $line <hr>"; } ?>
Any assistance much appreciated. Many thanks in advance.
-
Dave over 11 yearsI was going to mention
explode()
in my answer until I saw this post (then I just discarded my answer as this one is spot on). I highly recommend using explode rather than having another request. -
Martyn over 11 yearsThat is really great thanks! I had to modify the following line:
code
<font face='Arial'> $line </font><hr>";code
tocode
echo "<font face='Arial'> $line </font><hr>";code
but it works a treat! Thanks again. -
mickmackusa almost 3 yearsThe php manual has a specific note stating that strstr() and stristr() should not be used to search for checking the existence of a substring. php.net/manual/en/function.strstr.php