SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    845
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    The first function I ever made does not work

    I've never really had use for functions in the past, but now I do, so I'm trying to learn using a basic one I found on the web. Can someone explain why the word count is not being returned? I must be missing something very basic.

    Thanks!

    PHP Code:
    function count_words($str) {$words 0;$str eregi_replace(" +"" "$str);$array explode(" "$str);for($i=0;$i count($array);$i++){if (eregi("[0-9A-Za-z---]"$array[$i])) $words++;}return $words;}

    $str "hello there";
    count_words($str);
    echo 
    $words
    Convert your dollars into silver coins. www.convert2silver.com

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Basically to cut the reason short it's because your trying to call a variable in which doesn't exist, when using functions the return variable is just a value of whatever your returning there for you need to assign a variable to the function call itself.

    PHP Code:
    $words count_words($str);
    echo 
    $words
    On a side note your regular expressions shouldn't use the ereg(i) functions as they have been deprecated for a long time now, the preg family is what you should be using from now on. See the below code which i cleaned up a bit.

    PHP Code:
    function count_words($str) {
        
    $words explode(' 'preg_replace('/[ ]{2,}/'' '$str));
        
    $count 0;

        for (
    $i 0$i count($words); $i++) {
            if (
    preg_match('/[a-zA-Z0-9]+/'$words[$i])) {
                
    $count++;
            }
        }

        return 
    $count;
    }

    $str   'hello there';
    $total count_words($str);

    echo 
    'The total number of words is: ' $total
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,907
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    Even though @chris.upjohn 's answer is perfect, there is no need to reinvent the wheel: str_word_count
    Rmon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    845
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you both!
    Convert your dollars into silver coins. www.convert2silver.com


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
  •