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 7084319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:17:12+00:00 2026-05-28T07:17:12+00:00

I have a text logfile, containing lines of data separated by | for example

  • 0

I have a text logfile, containing lines of data separated by ” | “

for example

date | time | ip | geo-location (city) | page viewed ......

I need to find the 10 most occuring “page views” in the text file….

each log of page view is listed as:

//pageurl 

as the log is on seperate lines I am presuming I would be searching for the page urls between

// [url name] \r\n

how would I code a search to list the top 10 urls and list them into an array….

for example:

$url[0]  <<this would be the most occuring url
$url[1]  <<thos would be the second most occuring url

and so on….. until I can list them up to:

$url[9]  <<which would be the 10th most common url

I am not sure how I would search between the ” // ” and the ” \r\n ”

and then convert the top 10 most common occurrences into an array…

edit: here is 2x lines of my logs, just to help even more if I can

sunday, january 22, 2012 | 16:14:36 | 82.**.***.*** | bolton | //error 
sunday, january 22, 2012 | 17:12:52 | 82.**.***.*** | bolton | //videos
  • 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-28T07:17:13+00:00Added an answer on May 28, 2026 at 7:17 am

    Based on the information given, here’s a rather naïve approach:

    /* get the contents of the log file */
    $log_file = file_get_contents(__DIR__.'/log.txt');
    
    /* split the log into an array of lines */
    $log_lines = explode(PHP_EOL, $log_file);
    
    /* we don't need the log file anymore, so free up some memory */
    unset($log_file);
    
    /* loop through each line */
    $page_views = array();
    foreach ($log_lines as $line) {
        /* get the text after the last pipe character (the page view), minus the ' //' */
        $page_views[] = ltrim(array_pop(explode('|', $line)), ' /');
    }
    
    /* we don't need the array of lines either, so free up that memory */
    unset($log_lines);
    
    /* count the frequency of each unique occurrence */
    $urls = array_count_values($page_views);
    
    /* sort highest to lowest (may be redundant, I think array_count_values does this) */
    arsort($urls, SORT_NUMERIC);
    
    print_r($urls);
    /* [page_url] => num page views, ... */
    
    /* that gives you occurrences, but you want a numerical 
       indexed array for a top ten, so... */
    
    $top_ten = array();
    $i = 0;
    /* loop through the array, and store the keys in a new one until we have 10 of them */
    foreach ($urls as $url => $views) {
      if ($i >= 10) break;
      $top_ten[] = $url;
      $i++;
    }
    
    print_r($top_ten);
    /* [0] => page url, ... */
    

    ** Script output: **

    Array
    (
        [videos] => 1
        [error ] => 1
    )
    Array
    (
        [0] => videos
        [1] => error 
    )
    

    It’s not the most optimal solution, and the bigger your log file, the longer it’ll take. To this end, you may be better off logging to a database and querying from that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have text stored in SQL as HTML. I'm not guaranteed that this data
I have created a small program which logs text data to a file on
I have an HTML page that I want to insert text from a text-only
I have text file with a row like this: SendersTimeSeriesIdentification: COMPANY_A/COMPANY_B/REF_7/20090505 I'd like to
I have text like this format term: 156^^^:^^59 datainput Or term: 156^^^:59 datainput or
I have text within a paragraph tag that that fits snug on the bottom
I have text input boxes. There is validation for each of the boxes using
I have text I am displaying in SIlverlight that is coming from a CMS
I have text file with some stuff that i would like to put into
I have text file with something like first line line nr 2 line three

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.