Re: Finding a key word in a text file
Noam Dekers wrote:
>
> Hi all,
> I would like to find a word stored in a text file.
>
> the keyWords.txt file:
> -------------------------------
> Recording Site
[snip]
> The textOrigin.txt file:
> -------------------------------
> I found the INTRA inside
>
> My code:
> *******************
>
> PHP:--------------------------------------------------------------------------------
> <?php
>
> $filesource = "keyWords.txt";
> $fp = fopen ($filesource, "r");
>
> $storeWord = array();
>
> if ($fp)
> { $i = 0;
> while (!feof($fp))
> {
> /**********************************
> get the list of words to find and
> store them in an array for later use
> **********************************/
> $line = fgets ($fp, 100);
> $storeWord[$i] = $line;
>
> $i = $i+1;
The index isn't necessary, just do
$storeWord[] = $line;
The biggest problem is that you are storing lines, not keywords. In the
first place, many of these lines are multiple words. Is that what you
want? If so, then call them key phrases.
But most importantly, what about the newline at the end? That screws up
matching. See the manual:
fgets
(PHP 3, PHP 4 )
fgets -- Gets line from file pointer
Description
string fgets ( resource handle [, int length])
Returns a string of up to length - 1 bytes read from the file pointed to
by handle. Reading ends when length - 1 bytes have been read, on a
newline (which is included in the return value), or on EOF (whichever
comes first). If no length is specified, the length defaults to 1k, or
1024 bytes.
Remove whitespace from the end with chop().
> /**********************************
> open the text source file, pick each line
> and compare it to the complete list of key words
> **********************************/
Again, even with removing the newline you have phrases not words. If you
want words, you'll need more processing.
Brian Rodenborn
|