feedyourself Posted October 19, 2010 Share Posted October 19, 2010 I'm pretty sure this is really simple to do using the count function, but I am having a hard time finding examples on how to count the number of times a "SPECIFIC" word occurs in a file. For example, I just want to display how many times "this-word" occurs in "http://www.mysite.com/logfile.log". Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/216283-number-of-instances-of-a-specific-word-in-a-log-file/ Share on other sites More sharing options...
AbraCadaver Posted October 19, 2010 Share Posted October 19, 2010 One way: $count = preg_match_all('#\bsomeword\b#', file_get_contents('logfile.log'), $matches); Quote Link to comment https://forums.phpfreaks.com/topic/216283-number-of-instances-of-a-specific-word-in-a-log-file/#findComment-1124003 Share on other sites More sharing options...
salathe Posted October 19, 2010 Share Posted October 19, 2010 You could also use substr_count() for seeing how many times this-word appears, if there is no need for a regular expression like above. If the log file is big, then it might be better to move through the file a piece at a time and tally up the number of occurences of the word. Quote Link to comment https://forums.phpfreaks.com/topic/216283-number-of-instances-of-a-specific-word-in-a-log-file/#findComment-1124018 Share on other sites More sharing options...
AbraCadaver Posted October 19, 2010 Share Posted October 19, 2010 You could also use substr_count() for seeing how many times this-word appears, if there is no need for a regular expression like above. If the log file is big, then it might be better to move through the file a piece at a time and tally up the number of occurences of the word. Forgot about that one. Probably much faster also. But you may have problems with the word being part of a larger word, etc. So you would want to add a space before and after, but then you have problems if the word is at the beginning or end of a line, etc... Depends on what you want. Quote Link to comment https://forums.phpfreaks.com/topic/216283-number-of-instances-of-a-specific-word-in-a-log-file/#findComment-1124026 Share on other sites More sharing options...
salathe Posted October 19, 2010 Share Posted October 19, 2010 Depends on what you want. A key point. Lets see if the OP gives any feedback. Quote Link to comment https://forums.phpfreaks.com/topic/216283-number-of-instances-of-a-specific-word-in-a-log-file/#findComment-1124030 Share on other sites More sharing options...
feedyourself Posted October 19, 2010 Author Share Posted October 19, 2010 Thanks for all your feedback. This helps quite a bit. Quote Link to comment https://forums.phpfreaks.com/topic/216283-number-of-instances-of-a-specific-word-in-a-log-file/#findComment-1124058 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.