www.webdeveloper.com
Results 1 to 10 of 10

Thread: Is there any way to count how many times a specific word comes up in a txt file?

  1. #1
    Join Date
    Jun 2011
    Location
    UK
    Posts
    5

    Question Is there any way to count how many times a specific word comes up in a txt file?

    The title says it all. Thanks in advance.

  2. #2
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    search the manual for string functions.
    and if they're not powerfull enough you can always use regular expressions
    (something like this:

    PHP Code:
    $str "to be or not to be";
    preg_match_all("/\bbe\b/i"$str$matches);
    echo 
    count($matches[0]); 
    )
    Free Scripts (PHP, Javascript, MySQL), homemade in the Excudo Devshed
    Create your own personal Startpage for free

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,336
    Should be pretty straight-forward with something like what themarty suggested. Where it can get kind of "messy" is if you search for "play", for instance, and whether or not you want to find "plays", "played", and "playing" yet might not want to match on "playbill".
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Jun 2011
    Location
    UK
    Posts
    5
    my php knowledge is basic so could you expand more, thanks
    Last edited by last1here; 06-15-2011 at 08:44 AM.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,336
    Quote Originally Posted by last1here View Post
    my php knowledge is basic so could you expand more, thanks
    It's not so much a question of PHP knowledge (at this point) as it is deciding what your exact functional requirements are. Do you only want to match exact words, or do you want to match if the search word is contained within a larger word -- or something more complex such as matching only with certain suffixes such as "s", "es", "er", "ed" or "ing"? Should the searching be case-insensitive? Is there some other requirement I haven't thought of?

    Once you nail down the exact functional requirements, it's then much easier to come up with an exact coding solution.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Jun 2011
    Location
    UK
    Posts
    5
    I would only like to find exact words. The reason i want to find exact words is because i have a download counter that outputs into a .txt document and i would like to display how many times something has been downloaded.

    Eg. say i have a file called example.zip every time somebody downloads it it adds a line to the txt file saying: example.zip and the date.

    so i wanted to see how many times it says example so i can say how many times its been downloaded.

  7. #7
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    If you can make a database connection is would be much wiser to store this information there instead of a text-file.
    also be careful with your concept of words. if someone downloads example.zip and example.txt (or even Example.zip) you probably don't want to count them as hits belonging to one and the same file
    Free Scripts (PHP, Javascript, MySQL), homemade in the Excudo Devshed
    Create your own personal Startpage for free

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,336
    Quote Originally Posted by last1here View Post
    I would only like to find exact words. The reason i want to find exact words is because i have a download counter that outputs into a .txt document and i would like to display how many times something has been downloaded.

    Eg. say i have a file called example.zip every time somebody downloads it it adds a line to the txt file saying: example.zip and the date.

    so i wanted to see how many times it says example so i can say how many times its been downloaded.
    themarty's solution in the first reply should do the job then. (Remove the "i" after the final "/" of the regular expression if you want the matching to be case-sensitive.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Jun 2011
    Location
    UK
    Posts
    5

    Thumbs up

    Quote Originally Posted by themarty View Post
    If you can make a database connection is would be much wiser to store this information there instead of a text-file.
    also be careful with your concept of words. if someone downloads example.zip and example.txt (or even Example.zip) you probably don't want to count them as hits belonging to one and the same file
    I did what you said and made a db connection and just out putted the data that way.

    Thanks themarty & NogDog

  10. #10
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    i hope you are generating the results using a query now.
    just to be sure, you can now do something like this:

    Code:
    SELECT
    	COUNT(*) AS total, filename
    FROM downloads
    GROUP BY filename
    Free Scripts (PHP, Javascript, MySQL), homemade in the Excudo Devshed
    Create your own personal Startpage for free

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles