Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8811623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:24:41+00:00 2026-06-14T03:24:41+00:00

I am trying to search a text file for two values on a line.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:24:42+00:00Added an answer on June 14, 2026 at 3:24 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a djapian based full text search for searching user
I'm a rookie trying to search a text file for some serial numbers. I'm
Im trying to search through a text file and find the valid email addresses.
Im trying to write batch file code to check whether given line is present
I am trying to search a text file for a certain pattern. If this
I'm trying to write a perl script to search through a text file, find
I am trying to put full text search working on SQL Server 2008, however
I am trying to add full text search capabilities to my RoR app, but
I am trying to full text search a column, but also be able to
I am trying to index a table for full text search in a SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.