LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-14-2008, 02:40 AM   #1
andie87
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"

result:
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
Nylex
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
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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.

jlinkels
 
  


Reply

Tags
file, php, word



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:23 AM.

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