PHP str_word_count() 函数
PHP 教程
· 2019-01-30 06:28:08
实例
计算字符串 "Hello World!" 中的单词数:
<?php
echo str_word_count("Hello world!");
?>
echo str_word_count("Hello world!");
?>
定义和用法
str_word_count() 函数计算字符串中的单词数。
语法
str_word_count(string,return,char)
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
return | 可选。规定 str_word_count() 函数的返回值。 可能的值:
|
char | 可选。规定被认定为单词的特殊字符。 |
技术细节
返回值: | 返回一个数字或者一个数组,取决于所选择的 return 参数。 |
---|---|
PHP 版本: | 4.3.0+ |
更新日志: | 在 PHP 5.1 中,新增了 char 参数。 |
更多实例
实例 1
返回包含字符串中的单词的数组:
<?php
print_r(str_word_count("Hello world!",1));
?>
print_r(str_word_count("Hello world!",1));
?>
实例 2
返回一个数组,其中的键名是单词在字符串中的位置,键值是实际的单词:
<?php
print_r(str_word_count("Hello world!",2));
?>
print_r(str_word_count("Hello world!",2));
?>
实例 3
没有 char 参数和有 char 参数:
<?php
print_r(str_word_count("Hello world & good morning!",1));
print_r(str_word_count("Hello world & good morning!",1,"&"));
?>
print_r(str_word_count("Hello world & good morning!",1));
print_r(str_word_count("Hello world & good morning!",1,"&"));
?>
点击查看所有 PHP 教程 文章: https://www.codercto.com/courses/l/5.html
Never Lost Again
[美] Bill Kilday / HarperBusiness / 2018-5-29 / USD 8.00
As enlightening as The Facebook Effect, Elon Musk, and Chaos Monkeys—the compelling, behind-the-scenes story of the creation of one of the most essential applications ever devised, and the rag-tag tea......一起来看看 《Never Lost Again》 这本书的介绍吧!