home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomePHP CookbookSearch this book

1.4. Processing a String One Character at a Time

1.4.2. Solution

Loop through each character in the string with for. This example counts the vowels in a string:

$string = "This weekend, I'm going shopping for a pet chicken.";
$vowels = 0;
for ($i = 0, $j = strlen($string); $i < $j; $i++) {
    if (strstr('aeiouAEIOU',$string[$i])) {
        $vowels++;
    }
}


Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.