Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 11-14-2008, 02:40 AM   #1
LQ Newbie
Registered: Nov 2008
Posts: 1

Rep: Reputation: 0
Smile how to count each word in file using php?

example i've this file:

" gold silver silver truck"

gold = 1
silver = 2
truck = 1

how to do that?

how to count each word in file using pphp?
Old 11-14-2008, 02:46 AM   #2
LQ Addict
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You probably want to create a map, which allows you to associate keys and values - you can use your strings as the keys and their counts as the values. See the documentation about maps. Obviously you'll need to get a list of all the strings in your file first. As far as actually reading from a file goes, you'll probably want to use fopen() and fread() to do that. Again see the PHP documentation for information on these functions.

Last edited by Nylex; 11-14-2008 at 02:50 AM.
Old 11-14-2008, 02:30 PM   #3
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,632

Rep: Reputation: 711Reputation: 711Reputation: 711Reputation: 711Reputation: 711Reputation: 711Reputation: 711
Do they give homework assignment in PHP nowadays?

Anyway, you COULD study array use in PHP. For each word you read you look up if the index exists. If so, increase the value of the element with that index, if not, create one and set the value to 1.

Notes on fread etc by Nylex are valid of course.



file, php, word

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to delete last number/word of a file and incude file count at the end of the chennaiguy Linux - Newbie 2 02-18-2008 09:08 PM
word count issue George2 Programming 6 11-27-2007 06:11 AM
Word count with grep DiagonalArg Linux - Software 3 02-13-2006 12:46 PM
word count pantera Programming 2 08-31-2004 07:23 AM
word count in a line pantera Programming 4 08-25-2004 01:14 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:38 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration