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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:21:45+00:00 2026-06-01T12:21:45+00:00

I have a PHP search script which I’ve altered slightly and works well for

  • 0

I have a PHP search script which I’ve altered slightly and works well for me. But there is one part Im not sure how to do, and that is linking the script up to a search form, that a user can complete.

The script is below, and it searches a text file for key words. Currently the “key word” is entered directly into the script. But that is no good for someone visiting my site – so I wanted to create a search form. But not sure if I should use POST or GET or something else. And I dont know how I should link the code up from the form, to the script below. I’ve searched for quite a bit of time to find this, but cant see anything that covers it, and my attempts at getting it to link up arent going well. If anyone could help it would be most appreciated.

(original code borrowed from Lekensteyn – https://stackoverflow.com/a/3686246/1322744 (thanks))

<?php
$file = 'users.txt';
$searchfor = 'entersearchterm';

$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";

if(preg_match_all($pattern, $contents, $matches)){
   echo "Found matches:<br />";
   echo implode("<br />", $matches[0]);
}
else{
   echo "No matches found";
fclose ($file); 
}
?>
  • 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-01T12:21:47+00:00Added an answer on June 1, 2026 at 12:21 pm

    Code (a rough idea) :

    <html>
          <head><title>Search Form</title></head>
          <body>
                <form action="search.php" method="GET">
                       <input type="text" name="keyword" id="keyword width="50" value="" />
                       <input type="submit" value="Search"/>
                </form>
          </body>
    </html>
    

    And get the keyword from your PHP script like this :

    <?php
    // script.php
    
    
    $searchfor = $_GET['keyword'];
    
    $file = 'users.txt';
    
    $contents = file_get_contents($file);
    $pattern = preg_quote($searchfor, '/');
    $pattern = "/^.*$pattern.*\$/m";
    
    if(preg_match_all($pattern, $contents, $matches)){
       echo "Found matches:<br />";
       echo implode("<br />", $matches[0]);
    }
    else{
       echo "No matches found";
    fclose ($file); 
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP search suggestion script which uses a MySQL database as its'
I have a PHP search suggestion script which uses MySQL as its back-end. I
I have the following php script which works flawlessly in normal circumstances (i.e. visiting
I have a PHP script that builds a binary search tree over a rather
I have a PHP script that's supposed to take in a URL and search
I have a php script which constructs an array of objects based on HTML
I have a script called notify_me.php which I run like so: script type=text/javascript> //Run
I have a PHP search script that parses the results through HTML to be
I have a jQuery search script which uses the on keyup function to submit
I have a PHP scraper script which I use to scrape a page on

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.