1

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.

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I 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.
3

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>

2 Comments

That is really great thanks! I had to modify the following line: code <font face='Arial'> $line </font><hr>"; code to code echo "<font face='Arial'> $line </font><hr>"; code but it works a treat! Thanks again.
The 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
1

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>"; 
        } 

       ?>

Comments

0

This worked for me. You may define what you like in searchthis aray and it will be displayed with whole line.

<?php
$searchthis = array('1','2','3');
$matches = array();

$handle = fopen("file_path", "r");
if ($handle)
{
while (!feof($handle))
{
    $buffer = fgets($handle);

    foreach ($searchthis as $param) {
    if(strpos($buffer, $param) !== FALSE)
        $matches[] = $buffer;
 }}
 fclose($handle);
 }

 foreach ($matches as $parts) {
echo $parts;
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.