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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:08:15+00:00 2026-05-28T19:08:15+00:00

I need to get an end result which loads a txt file counts how

  • 0

I need to get an end result which loads a txt file counts how many times each word has been used and echos the results. The code is used is given below.Looking forward for valuable suggestions…

<?php   
 $text = fopen("words.txt", "r"); 
 $textarray = explode(" ",$text); 
 foreach($textarray as $numbers) 
    { 
     if(isset($str_count[$numbers])) 
     $str_count[$numbers]++; 
        else
         $str_count[$numbers]=1; 
    } 

 foreach($str_count as $words => $numbers) 
 echo $words.": ".$numbers."<br>"; 

 ?> 
  • 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-28T19:08:17+00:00Added an answer on May 28, 2026 at 7:08 pm

    You almost got it, but there are a few things that need to be changed.

    fopen() function opens up a file (in this case for reading) returns a resource (a handle of the file), which we use to read the file. It DOES NOT return file contents. If you need further information, please check fopen() documentation.
    I substituted fopen() with file_get_contents() just for the sake of simplicity.

    Secondly, as @DaveRandom suggested, it would be a good idea to substitute explode() for preg_split('/\s+/', $text);, because that way it would be able to deal with multiple spaces. Of course, this is not necessary, but recommended.

    And lastly, I found that with preg_split('/\s+/', $text) the script had an empty element, thus I added an if statement to make sure we do not add empty strings. This step is also not required, so if you do not need it, just remove the first if statement.

    And here is the modified source code:

    <?php   
    $text = file_get_contents('words.txt');
    $textarray = preg_split('/\s+/', $text);
    foreach($textarray as $numbers) 
    { 
        if(empty($numbers)) {
            continue;
        }
        if(isset($str_count[$numbers])) 
            $str_count[$numbers]++; 
        else
                $str_count[$numbers]=1; 
    } 
    
    foreach($str_count as $words => $numbers) 
        echo $words.": ".$numbers."<br>"; 
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get the exact start and end dates (month, day and year)
I need to get all substrings from string. For ex: StringParser.GetSubstrings([start]aaaaaa[end] wwwww [start]cccccc[end], [start],
i have text like this div bla-bla end div i need to get only
Need to get the 10 word before and 10 words after for the given
I'm doing video analysis. The end result array I get is something like: signal
everyone. I need to parse a webpage which has java cookies set for every
Just need get some vals located in application.ini(main ini) in the Controller plugin I
I need get all items these have no categories int? categoryId = null; var
I'm working with C#.net developing applications for windows mobile 6, and i need get
I have two tables with a weak relation. I need get a text value

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.