SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: strtok()

  1. #1
    SitePoint Zealot sethtrain's Avatar
    Join Date
    Dec 2003
    Location
    Paducah, KY
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    strtok()

    I am trying to get the number of vowels in the string how can I make this work? I always get one more that I should.

    PHP Code:
            $numvow ;
            
    $delims "'a''e''i''o''u'";
            
    $vows strtok ($str$delims);
            while (
    is_string ($vows)){
                if (
    $vows)
                
    $numvow $numvow 1;
                
    $vows strtok ($delims);
            }
            
    $msg "The number of vowels in your paragraph(s) are: $numvow. <br>(Please use Back Button for more testing...)";
            break; 
    If I type:

    Code:
    This is a test. This is a test.
    I get:
    Code:
    The number of sentences in your paragraph(s) are: 9.
    (Please use Back Button for more testing...)
    Can anyone help me with this?
    --
    sethtrain

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Grapevine
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    start with $numvow = -1;

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    strtok is a wrong tool for this. You'll get incorrect results if input string starts or ends with a vowel.

    preg_match would be probably better:

    PHP Code:
    $num_vowels preg_match_all("~[aeiou]~"$str); 


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
  •