首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

str_word_count

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

str_word_count - 返回关于字符串中使用的单词的信息

描述

代码语言:javascript
复制
mixed str_word_count ( string $string [, int $format = 0 [, string $charlist ]] )

计算字符串内的字数。 如果未指定可选格式,则返回值将是表示找到的单词数的整数。 在格式被指定的情况下,返回值将是一个数组,其内容取决于格式。 下面列出了格式和结果输出的可能值。

为了这个功能的目的,'word'被定义为一个包含字母字符的区域依赖字符串,它也可能包含,但不以' ' “和” - “字符开始。

参数

string

字符串

format

指定此函数的返回值。目前支持的值是:

  • 0 - 返回找到的词汇数量
  • 1 - 返回一个数组,其中包含在其中找到的所有字符 string
  • 2 - 返回一个关联数组,其中键是字符串中单词的数字位置,值是实际单词本身

charlist

将被视为“单词”的附加字符列表

返回值

根据format所选内容返回一个数组或整数。

更新日志

描述

5.1.0

添加了charlist参数

例子

示例#1 str_word_count()示例

代码语言:javascript
复制
<?php

$str?=?"Hello?fri3nd,?you're
???????looking??????????good?today!";

print_r(str_word_count($str,?1));
print_r(str_word_count($str,?2));
print_r(str_word_count($str,?1,?'àá??3'));

echo?str_word_count($str);

?>

上面的例子将输出:

代码语言:javascript
复制
Array
(
    [0] => Hello
    [1] => fri
    [2] => nd
    [3] => you're
    [4] => looking
    [5] => good
    [6] => today
)

Array
(
    [0] => Hello
    [6] => fri
    [10] => nd
    [14] => you're
    [29] => looking
    [46] => good
    [51] => today
)

Array
(
    [0] => Hello
    [1] => fri3nd
    [2] => you're
    [3] => looking
    [4] => good
    [5] => today
)

7

扩展内容

  • explode() - 按字符串拆分字符串
  • preg_split() - 用正则表达式分割字符串
  • split() - 通过正则表达式将字符串拆分为数组
  • count_chars() - 返回有关字符串中使用的字符的信息
  • substr_count() - 计算子串出现次数

← str_split

strcasecmp →

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com