Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1048595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:28:57+00:00 2026-05-16T16:28:57+00:00

I got a custom log, roughly 29MBs of user’s data, including user agent. I

  • 0

I got a custom log, roughly 29MBs of user’s data, including user agent. I want to parse through it (essentially just search) and find how many occurances of say, “Firefox” or “MSIE” appear in it, like a mini log parser.

This is where I am stumped.. What I was getting at is explode()ing newlines, and iterate through the array, use:

if stripos($line, 'Firefox') $ff++;" 

or something stupid but I realize that would take up a lot of memory/use a lot of functions.

What would be a good way to list number of occurances?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:28:58+00:00Added an answer on May 16, 2026 at 4:28 pm

    You’ll want to read the file line by line to avoid using up memory with lots of data.

    $count = array('Firefox' => 0, 'MSIE' => 0, 'Others' => 0);
    $handle = fopen("yourfile", "r");
    
    if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle, 4096);
    
            // actual counting here:
            if (stripos($buffer, 'Firefox')) {
                $count['Firefox']++;
            } else if (stripos($buffer, 'MSIE')) {
                $count['MSIE']++;
    
            // this might be irrelevant if not all your lines contain user-agent
            // strings, but is here to show the idea
            } else {
                $count['Others']++; 
            }
        }
        fclose($handle);
    }
    
    print_r($count);
    

    Also depending on the format of your file (which wasn’t supplied), you might want to use regex or a more refined method to count occurences, eg:

    $count = array('Firefox' => 0, 'MSIE' => 0, 'Others' => 0);
    $handle = fopen("yourfile", "r");
    
    if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle, 4096);
            $ua = get_user_agent($buffer);  
            $count[$ua]++;
        }
        fclose($handle);
    }
    
    print_r($count);
    
    /* @param $line
     * @return string representing the user-agent
     *
     * strpos() works for the most part, but you can use something more 
     * accurate if you want
     */
    function get_user_agent($line) {
        // implementation left as an exercise to the reader
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to set a custom log format for nginx, so that request got parsed
I got a custom Data type named Student which has marks for 2 subjects.
I've got a custom attribute that I want to apply to my base abstract
I've got an installer with a custom action project. I want the action to
I've got a custom NSView and I want to receive the pinch to zoom
I have got a code from a tutorial How to parse JSON data into
I've got a custom OpenID provider that I'd like to use to log in
I've got a custom ant script for building and other fun stuff. IntelliJ has
I've got a custom component that has children components dynamically added and removed to
I've got a custom javascript autocomplete script that hits the server with multiple asynchronous

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.