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 8980249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:02:17+00:00 2026-06-15T20:02:17+00:00

I wonder if anyone can help with a little problem I can’t seem to

  • 0

I wonder if anyone can help with a little problem I can’t seem to fix – my
head is going round in circles at the moment…

Ok I have a .txt file with numerous lines of info – I am trying to match keywords
with those lines and display a certain number of the matching lines.

I put together this bit of script and whilst it works it only matches a line if the
words are in the same order as the search words.

At the moment as an example:

Search words:

red hat

Lines in .txt file:

this is my red hat
my hat is red
this hat is green
this is a red scarf
your red hat is nice

As the script is at the moment it will match and display lines 1, 5

However I would like it to match and display lines 1, 2, 5

Any order but all words must be present to match.

I have looked through loads of postings here and elsewhere and I understand that
what is needed is to explode the string and then search for each word in a loop but
I cannot get that to work, despite trying a few different ways as it just returns the
same line numerous times.

Any help would be appreciated before I lose what hair I have left 🙂

Here is the code I have working at present – the search variable is already
set:

<?php
rawurldecode($search);
$search = preg_replace('/[^a-z0-9\s]|\n|\r/',' ',$search);
$search = strtolower($search);
$search = trim($search);

$lines = file('mytextfile.txt') or die("Can't open file");
shuffle($lines);

$counter = 0;

// Store true when the text is found
$found = false;

foreach($lines as $line)
 {

  if(strpos($line, $search) !== false AND $counter <= 4)
  {
    $found = true;
    $line = '<img src=""> <a href="">'.$line.'</a><br>';


    echo $line;
    $counter = $counter + 1;

  }

}

// If the text was not found, show a message
if(!$found)
{
  echo  $noresultsmessage;
}

?>

Thanks in advance for any help – still learning 🙂

  • 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-15T20:02:19+00:00Added an answer on June 15, 2026 at 8:02 pm

    Here’s my code:

    $searchTerms = explode(' ', $search);
    $searchCount = count($searchTerms);
    foreach($lines as $line)
     {
        if ($counter <= 4) {
            $matchCount = 0;
            foreach ($searchTerms as $searchWord) {
                if (strpos($line, $searchWord) !== false ) {
                    $matchCount +=1;
                } else {
                    //break out of foreach as no need to check the rest of the words if one wasn't found
                    continue; 
                }
            }
            if ($matchCount == $searchCount) {
                $found = true;
                $line = '<img src=""> <a href="">'.$line.'</a><br>';
                echo $line;
                $counter = $counter + 1;
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wonder if anyone can help me with this problem. I have created a Window
I wonder if anyone can help - I have this error showing recently when
I wonder if anyone can help me to understand where I could be going
I wonder if anyone can help with a jQuery problem I am having. I
I have a VC++ Win32 DLL project. I wonder if anyone can help me
wonder if anyone can help; I have an a two dimensional array which I
I wonder if anyone can help with the following. I have integrated both iAds
I wonder is anyone can help me with this annoying problem. Trying to insert
Wonder if anyone can help me. I have a setup whereby a main menu
I wonder if anyone can help? I have a list with a collection of

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.