Prev | Current Page 336 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

If format is defined, it modifies the function??™s behavior based on its value:
1: Returns an array consisting of all words located in str.
2: Returns an associative array, where the key is the numerical position of the
word in str, and the value is the word itself.
Consider an example:
CHAPTER 9 ?–  S TRINGS AND REGULAR EXPRESS IONS 273
$summary = <<< summary
In the latest installment of the ongoing Developer.com PHP series,
I discuss the many improvements and additions to PHP 5's
object-oriented architecture.
summary;
$words = str_word_count($summary);
printf("Total words in summary: %s", $words);
?>
This returns the following:
Total words in summary: 23
You can use this function in conjunction with array_count_values() to determine
the frequency in which each word appears within the string:
$summary = <<< summary
In the latest installment of the ongoing Developer.com PHP series,
I discuss the many improvements and additions to PHP 5's
object-oriented architecture.
summary;
$words = str_word_count($summary,2);
$frequency = array_count_values($words);
print_r($frequency);
?>
This returns the following:
Array ( [In] => 1 [the] => 3 [latest] => 1 [installment] => 1 [of] => 1
[ongoing] => 1 [Developer] => 1 [com] => 1 [PHP] => 2 [series] => 1
[I] => 1 [discuss] => 1 [many] => 1 [improvements] => 1 [and] => 1
[additions] => 1 [to] => 1 [s] => 1 [object-oriented] => 1
[architecture] => 1 )
274 CHAPTER 9 ?–  STRINGS AND REGULAR EXPRESSIONS
Taking Advantage of PEAR: Validate_US
Regardless of whether your Web application is intended for use in banking, medical,
IT, retail, or some other industry, chances are that certain data elements will be
commonplace.


Pages:
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348