Dizzee15 Posted July 3, 2009 Share Posted July 3, 2009 Is there any function that would find specified word in another file. Lets say we got file called information.txt : one=1|two=2|three=3 Now, first lets say I want to check if there is a word "two" in that file, then find it's number - "2". I need code. Link to comment https://forums.phpfreaks.com/topic/164673-search-for-a-word/ Share on other sites More sharing options...
flyhoney Posted July 3, 2009 Share Posted July 3, 2009 <?php // First, read file into an array $lines = file('information.txt'); $information = array(); foreach ($lines as $line) { $words = explode('|', $line); foreach ($words as $word) { list($term, $count) = explode('=', $word); if (isset($information[$term])) { $information[$term] += $count; } else { $information[$term] = $count; } } } // And now you can search the array for words. $word_to_find = 'two'; if (isset($information[$word_to_find])) { echo "$word_to_find is in the file and it's count is {$information[$word_to_find]}"; } Link to comment https://forums.phpfreaks.com/topic/164673-search-for-a-word/#findComment-868396 Share on other sites More sharing options...
Dizzee15 Posted July 3, 2009 Author Share Posted July 3, 2009 Thanks . Link to comment https://forums.phpfreaks.com/topic/164673-search-for-a-word/#findComment-868400 Share on other sites More sharing options...
Dizzee15 Posted July 3, 2009 Author Share Posted July 3, 2009 Ohh and by the way. echo "$word_to_find is in the file and it's count is {$information[$word_to_find]}"; Are "{}" really needed, what for ? Link to comment https://forums.phpfreaks.com/topic/164673-search-for-a-word/#findComment-868412 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.