01c12-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 43 行

PHP
43
字号
<?php// A function to return a count of all words used in the stringfunction full_count_words($str) {	// Use str_word_count passing it a '1' so it breaks words out for us	$words = str_word_count($str, 1);		// Loop over all elements of this array forming a new array that uses	// the word as a key and counts how many times each one is found	$results = array();	foreach ($words as $w) {		// Convert the entire word to lowercase,		//  so that 'The' and 'the' will still match		$lw = strtolower($w);		// If it's the first time we have seen this word,		//  add it to the array, else increment it		if (!(isset($results[$lw]))) {			$results[$lw] = 1;		} else {			$results[$lw]++;		}	}		// We now have the count we wanted, return it	return $results;}$test_string = "There are many top secret chemical compounds that we aretrying to keep a secret from the director of the institute.  Perhaps thesecret society could hold a meeting and ask all the members to attend anddiscuss this issue.";// Determine the word count:$wordcount = full_count_words($test_string);// First echo out the number of times the word 'secret' is used:// prints: The word secret was used 3 times.echo '<pre>';echo "The word secret was used {$wordcount['secret']} times.\n\n";// Now go ahead and echo out the entire array:print_r($wordcount);echo '</pre>';?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?