php str_word_count,PHP之string之str_word_count()函数使用

str_word_count

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

str_word_count — Return information about words used in a string

str_word_count — 返回字符串中单词的使用情况

Description

mixed str_word_count (

string $string [,

int $format = 0 [,

string $charlist ]]

)

/**

Counts the number of words inside string. If the optional format is not specified, then the return value will be an integer representing the number of words found. In the event the format is specified, the return value will be an array, content of which is dependent on the format. The possible value for the format and the resultant outputs are listed below.

统计 string 中单词的数量。如果可选的参数 format 没有被指定,那么返回值是一个代表单词数量的整型数。如果指定了 format 参数,返回值将是一个数组,数组的内容则取决于 format 参数。format 的可能值和相应的输出结果如下所列。

For the purpose of this function, 'word' is defined as a locale dependent string containing alphabetic characters, which also may contain, but not start with "'" and "-" characters.

对于这个函数的目的来说,单词的定义是一个与区域设置相关的字符串。这个字符串可以包含字母字符,也可以包含 "'" 和 "-" 字符(但不能以这两个字符开始)。

*/

Parameters

string

The string

字符串。

format

Specify the return value of this function. The current supported values are:

指定函数的返回值。当前支持的值如下:

0 - returns the number of words found 返回单词数量

1 - returns an array containing all the words found inside the string 返回一个包含 string 中全部单词的数组

2 - returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself 返回关联数组。数组的键是单词在 string 中出现的数值位置,数组的值是这个单词

charlist

A list of additional characters which will be considered as 'word'

附加的字符串列表,其中的字符将被视为单词的一部分。

Return Values

Returns an array or an integer, depending on the format chosen.

返回一个数组或整型数,这取决于 format 参数的选择。

Changelog

5.1.0 Added the charlist parameter

Examples

/**

* Created by PhpStorm.

* User: zhangrongxiang

* Date: 2018/3/8

* Time: 下午9:48

*/

$str = "Hello fri3nd, you're

looking good today!";

//[0] => Hello

//[1] => fri

//[2] => nd

//[3] => you're

//[4] => looking

//[5] => good

//[6] => today

print_r( str_word_count( $str, 1 ) );

//[0] => Hello

//[6] => fri

//[10] => nd

//[14] => you're

//[28] => looking

//[45] => good

//[50] => today

print_r( str_word_count( $str, 2 ) );

//[0] => Hello

//[1] => fri3nd

//[2] => you're

//[3] => looking

//[4] => good

//[5] => today

print_r( str_word_count( $str, 1, 'àáãç3' ) );

//[0] => Hello

//[1] => fri3nd

//[2] => you're

//[3] => looking

//[4] => good

//[5] => today

//[6] => look123

//[7] => ing

$str = "Hello fri3nd, you're

looking good today!

look1234ing";

print_r( str_word_count( $str, 1, '0..3' ) );

See

All rights reserved

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值