SitePoint Sponsor

User Tag List

Results 1 to 20 of 20

Thread: Count Words

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Count Words

    any one can give me a hint how to write a function that accepts a string and returns the number of words in that string?

  2. #2
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i believe there's a builtin called strlen($string)

  3. #3
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    believe it or not

    http://au2.php.net/manual/en/functio...word-count.php

    strlen will return the number of charachters rather than words

  4. #4
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ohhh, words. thanks for the correction. for some reason, i was thinking characters.

    str_word_count is new to me

  5. #5
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's okay, it's PHP >= 4.3.0.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even I couldn't use the posted hint, butl thanks alot platinum !
    Guyz, the function I was looking for must able to count how many words in astring.

  7. #7
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $count str_word_count($str); 
    $count contains the number of words in the string $str, what's the problem?

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any one can explain for me a function can count the words in string?

  9. #9
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gaheris
    PHP Code:
    $count str_word_count($str); 
    $count contains the number of words in the string $str, what's the problem?
    What don't you understand?

  10. #10
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, lets say it in this way:
    I should have a function do the following:
    1)Accepts a string
    2)Return the numbers of word in that string.
    Note: the string should not count the spaces in this string and return the number of spaces, since we could have double spaces between tow words.

    Example (1)
    String (Hi all hope you are fine)
    Output : 6 words

    Example (2)
    String (Hi all hope you are fine)
    Output : 6 words

  11. #11
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $str "Hi all hope you are fine";
    $count str_word_count($str); 
    echo 
    $count

  12. #12
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Marwan
    Ok, lets say it in this way:
    I should have a function do the following:
    1)Accepts a string
    2)Return the numbers of word in that string.
    Note: the string should not count the spaces in this string and return the number of spaces, since we could have double spaces between tow words.

    Example (1)
    String (Hi all hope you are fine)
    Output : 6 words

    Example (2)
    String (Hi all hope you are fine)
    Output : 6 words
    if you look at the function example at php.net, they do show that it ignores repeating spaces

    PHP Code:
    <?php

    $str 
    "Hello friend, you're
            looking          good today!"
    ;

    $a   str_word_count($str1);
    $b   str_word_count($str2);
    $c   str_word_count($str);

    print_r($a);
    print_r($b);
    print 
    $c;

    /* Output may look like:

    Array
    (
        [0] => Hello
        [1] => friend
        [2] => you're
        [3] => looking
        [4] => good
        [5] => today
    )

    Array
    (
        [0] => Hello
        [6] => friend
        [14] => you're
        [29] => looking
        [46] => good
        [51] => today
    )

    6   <------ 6 words

    */
    ?>
    if you are still having trouble with the function, show us a working example where it returns the wrong word count.

    Tested Example
    PHP Code:
    <?php
    $str1 
    "Hi all hope you are fine";
    $str2 "Hi        all hope       you are        fine";

    echo 
    str_word_count($str1).'<br />';
    echo 
    str_word_count($str2).'<br />';
    ?>
    output
    Code:
    6
    6
    i still don't see the problem with this function
    Last edited by GeekSupport; Oct 14, 2003 at 11:37.

  13. #13
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uh, didn't you read any posts that the others and I have written?

  14. #14
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm thinking the same thing, Gaheris

  15. #15
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    GeekSupport,
    Ive tried your example and I got the following error Fatal error: Call to undefined function: str_word_count() in this line
    Code:
    echo str_word_count($str1).'<br />';
    Please advise !

  16. #16
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Marwan
    GeekSupport,
    Ive tried your example and I got the following error Fatal error: Call to undefined function: str_word_count() in this line
    Code:
    echo str_word_count($str1).'<br />';
    Please advise !
    str_word_count is only available with PHP 4.3.0 >

    ....http://php.net is your friend....
    My name is Steve, and I'm a super-villian.

  17. #17
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    United States
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    function count_words ($string)
    {
     if (
    phpversion() >= '4.3.0') {
         
    $count str_word_count($string);
     } else {
      
    $count count(preg_split('/\W+/'$string, -1PREG_SPLIT_NO_EMPTY));
     }
     return 
    $count;
    }

    $word_count count_words('What are you all doin up here?');
    echo 
    $word_count;
    ?>

  18. #18
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great !
    I think the problem was with the version !
    tmapom, can you expalin for me this line

    PHP Code:
     $count count(preg_split('/\W+/'$string, -1PREG_SPLIT_NO_EMPTY)); 

  19. #19
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look up the preg_split function.

  20. #20
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    United States
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a page that helps: http://us3.php.net/manual/en/pcre.pattern.syntax.php
    preg_split basically will split the string into arrays where it maches the pattern ( \W+ ).
    \W = any "non-word" character
    + = 1 or more quantifier <- Not sure what this does exactly.

    Anyways, the -1 means there is no limit so it will put everything into an array. The PREG_SPLIT_NO_EMPTY is just something that will only allow non empty pieces. (SO space doesn't count and is not included in the value. Then you just have the count() function to return the number of results.


Bookmarks

Posting Permissions

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