str_word_count() 计算字符串中的单词数

实例

1.计算字符串 "Hello World!" 中的单词数:

<?php
echo str_word_count("Hello world!");
?>

输出
2

2.返回包含字符串中的单词的数组:

<?php
print_r(str_word_count("Hello world!",1));
?>
输出
Array
(
[0] => Hello
[1] => world
)

3.返回一个数组,其中的键名是单词在字符串中的位置,键值是实际的单词:

<?php
print_r(str_word_count("Hello world!",2));
?>
输出
Array
(
[0] => Hello
[6] => world
)

3.没有 char 参数和有 char 参数:

<?php
print_r(str_word_count("Hello world & good morning!",1));
print_r(str_word_count("Hello world & good morning!",1,"&"));
?>

输出
Array
(
[0] => Hello
[1] => world
[2] => good
[3] => morning
)
Array
(
[0] => Hello
[1] => world
[2] => &
[3] => good
[4] => morning
)

 


定义和用法

str_word_count() 函数计算字符串中的单词数。


语法

str_word_count(string,return,char)

参数描述
string必需。规定要检查的字符串。
return可选。规定 str_word_count() 函数的返回值。

可能的值:

  • 0 - 默认。返回找到的单词的数目。
  • 1 - 返回包含字符串中的单词的数组。
  • 2 - 返回一个数组,其中的键名是单词在字符串中的位置,键值是实际的单词。
char可选。规定被认定为单词的特殊字符。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值