Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search a .txt file

Status
Not open for further replies.

mkzw0ot4m0nk

Programmer
Jul 19, 2002
29
0
0
US
hey im kind of new to php. i was wondering how u can search a txt file to see if a word is in it like if the txt file looked like this..

monkey
donkey
bird
cat
dog

and the person typed monkey then it will return with yes monkey is in the list.

thanks
 
This should do it. It's not very effective though.
Code:
<?php
if (isset($_GET['string']))
{
    $contents = implode(&quot;&quot;, file(&quot;filename&quot;));
    if (strstr($contents, $_GET['string']))
    {
       echo $_GET['string'] . &quot; was found in the list.&quot;;
    }
}
?>
<form action=&quot;<?=$_SERVER['PHP_SELF']?>&quot; method=&quot;GET&quot;>
Search for: <input type=&quot;text&quot; name=&quot;string&quot; /><br />
<input type=&quot;submit&quot; value=&quot;Search the file&quot; />
</form>
//Daniel
 
Maybe even more interesting is that you could have your text file sorted according to entries and then do a binary search on it each time you wanted to know if the item existed in the file. This way you would have super-fast result times. support@interwebber.com if you are interested in details.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top