Jump to content

Recommended Posts

Hi all!

I've been working on an website which, I admit, is quite ambitious.  Especially seeing as though I am teaching myself PHP as I go.  ;) (So if I sound like a n00b... well, I am.  -to PHP that is, I do know Java.)

So, my problem is this:

I am trying to make a very basic search engine.  It's supposed to search a text file line-by-line for the search term (one per line), and tell the user if the term was in the file.  I tried making a function for this, but it turns out that it only reports back positively if the user was searching for the very last line, not any of the others.

Here's my code (basically, I edited out some extra stuff):

<?php
include("header.php");

function checkdb($search)
{
$file = fopen("data.txt", "r");
$match=false;
$current = "whatever";

while(!feof($file))
{
$current = fgets($file);
if(substr_compare($search, $current, 0) == 0)
{
$match = true;
}
}
return $match;
}

echo("<div align=\"center\">");
echo("<b>Search!</b><br>");
echo("<form action=\"search.php\" method=\"get\">");
echo("Enter your term here: <input type=\"text\" name=\"searchterm\">");
echo("<input type=\"submit\" value=\"Search\"></form></div><br><br><br>");

$search = $_GET["searchterm"];
$found = false;

if ($search == "")
{
echo("<title>Search</title>");
}

else
{
echo("<title>Search Results</title>
$found = checkdb($search);

if ($found == true)
{
echo("<br>Congrats, ".$search." is in our database!");
}
else
{
echo("<br>Sorry, ".$search." is not in our database.");
}

}
?>

 

Also, the format of the file data.txt is:

phrase1
phrase2
etc, etc.

 

Does anyone know what the problem is??

Thanks so much!

Link to comment
https://forums.phpfreaks.com/topic/55883-reading-text-files/
Share on other sites

Its because of the newline character at the end of each line (except the last line obviously). You can use rtrim() on $current to remove the newline character. Also heres an alternative method for you to ponder...

function checkdb($search)
{
    $lines = file("data.txt"); //reads the entire file into an array where each element is a line in the file
    
    foreach ($lines as $line) {
        if (strcmp($search, rtrim($line)) == 0) { 
            return true; //stop looking
        }
    }
    return false;
}

Link to comment
https://forums.phpfreaks.com/topic/55883-reading-text-files/#findComment-276144
Share on other sites

Here's another couple ideas to consider as optional ways to handle your search (depending on how large your text file is:

<?php
// Option 1: use file()
$text = 'data.txt';
$lines = file($text);
$match = false;
foreach ($lines as $line) {
  if (preg_match($pattern, $line)) $match = true;
}
if ($match) // your search was found
else // search was not found


// Option 2: use file_get_contents()
$text = 'data.txt';
$data = file_get_contents($text);
if (preg_match($pattern, $data)) $match = true;
else $match = false;
if ($match) // your search was found
else // search was not found
?>

Link to comment
https://forums.phpfreaks.com/topic/55883-reading-text-files/#findComment-276303
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.