pragan Posted August 29, 2008 Share Posted August 29, 2008 Hey Guys, I am trying to get the following code done but I am not able to proceed after some point. Please help me with this. I am trying to read a text file(contents is in this form: a,b,cde,fgh,) and then look for a string(obtained from a mysql query) in this text file. I am able to query the database and get the result, read the text file, explode it to get the words line by line but I am not able to match for the query result in the text file output. Please help me out Ex: If the query result has the value "cde" then I need to look in the text file and say word found else say not found. Waiting for any reply. regards pragan. Quote Link to comment https://forums.phpfreaks.com/topic/121857-solved-read-a-text-file-and-search-for-a-string/ Share on other sites More sharing options...
sasa Posted August 29, 2008 Share Posted August 29, 2008 look <?php $text = 'a,b,cde,fgh'; $word = 'cde'; $text = explode(',', $text); if (in_array($word, $text)) echo 'found'; else echo 'not found'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/121857-solved-read-a-text-file-and-search-for-a-string/#findComment-628691 Share on other sites More sharing options...
pragan Posted August 29, 2008 Author Share Posted August 29, 2008 thanks for the reply. I guess I have to use array_search as the text contents are stored in array format after I explode them. Again one more question : If I use array_search9$queryresult, $textcontents), its not doing the search but if I say the word directly array_search("testing", $textcontents) its able to say found/not found...Also I am getting the output like 7 times (i.e. found notfound not found..notfound..) I do not understand this. Please help me out. Quote Link to comment https://forums.phpfreaks.com/topic/121857-solved-read-a-text-file-and-search-for-a-string/#findComment-628721 Share on other sites More sharing options...
obsidian Posted August 29, 2008 Share Posted August 29, 2008 There are a couple things to consider. First, if you are just doing the loop, you need to break out of the loop when the string is found. That way, you don't continue to process and report on the rest of the file when you have already found the result. Also, if you use a string search, you can avoid having to worry about breaking the string into pieces. Try something like this: <?php function fileSearch($filename, $string) { $handle = fopen($filename, 'r'); while (!feof($handle)) { $row = fgets($handle, 4096); $pattern = '|\b' . $string . '\b|'; // Check for word boundaries if (preg_match($pattern, $row)) { // Break out of the loop fclose($handle); return TRUE; } } return FALSE; // nothing was found } if (fileSearch('filename.txt', 'my-string')) { // String appears in document } ?> Of course, if your text files are not super long, you can always just get the whole thing as a string and then do your boundary search on it: <?php $string = 'abc'; $text = file_get_contents('filename.txt'); if (strstr($text, $string)) { // At least one occurrence found. } ?> Good luck! Quote Link to comment https://forums.phpfreaks.com/topic/121857-solved-read-a-text-file-and-search-for-a-string/#findComment-628732 Share on other sites More sharing options...
pragan Posted August 29, 2008 Author Share Posted August 29, 2008 Thank you so much! Its so clear now for me to understand then implement it. It worked right! I will save this thread. Let me know if I need to make this thread as "Solved" thanks everyone! regards pragan. Quote Link to comment https://forums.phpfreaks.com/topic/121857-solved-read-a-text-file-and-search-for-a-string/#findComment-628785 Share on other sites More sharing options...
obsidian Posted August 29, 2008 Share Posted August 29, 2008 Any time you feel your question has been answered thoroughly, please do mark the thread as solved. If you have additional questions or clarification later, you can always come back and post more or start a new thread. Good luck! Quote Link to comment https://forums.phpfreaks.com/topic/121857-solved-read-a-text-file-and-search-for-a-string/#findComment-628796 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.