weebulgirl Posted June 16, 2007 Share Posted June 16, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/55883-reading-text-files/ Share on other sites More sharing options...
KrisNz Posted June 17, 2007 Share Posted June 17, 2007 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; } Quote Link to comment https://forums.phpfreaks.com/topic/55883-reading-text-files/#findComment-276144 Share on other sites More sharing options...
chigley Posted June 17, 2007 Share Posted June 17, 2007 Syntax error: #..... if ($search == "") { echo("<title>Search</title>"); } else { echo("<title>Search Results</title>"); // Needed to close echo here $found = checkdb($search); if ($found == true) #..... Quote Link to comment https://forums.phpfreaks.com/topic/55883-reading-text-files/#findComment-276256 Share on other sites More sharing options...
obsidian Posted June 17, 2007 Share Posted June 17, 2007 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/55883-reading-text-files/#findComment-276303 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.