Portal Home > Knowledgebase > Articles Database > Finding a key word/phrase in text file?


Finding a key word/phrase in text file?




Posted by P-nut, 06-24-2008, 05:11 PM
I know the answer for this must be pretty simple but I'm just not having much luck finding what I need. I have a PHP script with a form and a text file containing a bunch of keywords/phrases. I want to, upon the form submission, open the text file and search it to see if the contents of one of the fields match a phrase in the text file. The phrases are separated in the text file by returns (ie, one phrase per line) I'm pretty sure I'd use something like fopen to open the text file, but I have no clue where to go from there. Any help is always appreciated!

Posted by Jamie Edwards, 06-24-2008, 06:08 PM
http://uk2.php.net/file_get_contents http://uk.php.net/strpos Would be what I'd use

Posted by 111111z, 06-24-2008, 06:16 PM
test code will search in the string $a for 12 and return the found string position if found because PHP rulez

Posted by P-nut, 06-24-2008, 08:13 PM
I've tried file_get_contents with strpos and was unable to get it to work. I then tried this: $string should have had 2 phrase matches, but continues to show as 0.

Posted by 111111z, 06-24-2008, 08:58 PM
just change the if statement to this if(strpos($string,trim($line)) === false): //also tried switching $line and $string, also tried stripos for case-insensitive search that should work I think the reason why is because the CR LF from the file is making it not match... trim removes them..

Posted by P-nut, 06-24-2008, 09:15 PM
thanks so much 111111z! the trim did the trick and now returned the proper number of matches now my code looks like this:

Posted by 111111z, 06-24-2008, 09:29 PM
No Problem I have run into that same problem before that the cr lf is screwing things up.... php rulez



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
Deasoft Down (Views: 843)


Language: