资讯专栏INFORMATION COLUMN

PHP之string之str_word_count()函数使用

liukai90 / 1539人阅读

摘要:返回字符串中单词的使用情况统计中单词的数量。如果可选的参数没有被指定,那么返回值是一个代表单词数量的整型数。如果指定了参数,返回值将是一个数组,数组的内容则取决于参数。指定函数的返回值。

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
 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

http://php.net/manual/zh/func...

All rights reserved

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/28367.html

相关文章

  • PHP开发手册》笔记PHP中关于字符串的操作函数

    摘要:字符串分解操作要进行分解的字符串分解的长度。获取字符串的长度函数要进行长度计算的字符串包括首尾空格获取字符串的子串要进行截取的字符串截取开始的字符位置可选,要截取的字符串长度。默认从开始到结尾,字符串的第一个位置为获取字符串的子串 一 print和echo print 1)语法 int print(str);//str--要输出的字符串,返回值永远为1 --语法1 p...

    lingdududu 评论0 收藏0
  • 10个你或许不了解但实用的PHP函数

    摘要:使用方法当你想要把代码显示到页面上时,函数就会非常有用,它可以用内置定义的语法高亮颜色把你提供的代码高亮显示。使用方法这是一个非常有用的函数,它能返回指定的文件,并按照语法高亮突出显示文件内容。 PHP的功能越来越强大,里面有着非常丰富的内置函数。资深的PHP程序员对它们可能都很熟悉,但很多的PHP学习者,仍然对一些非常有用的函数不太熟悉。 这篇文章里,我们就列举10个你或许不了解但实...

    RdouTyping 评论0 收藏0
  • PHP中比较有用的几个函数

    摘要:当你想要把代码显示到页面上时,函数就会非常有用,它可以用内置定义的语法高亮颜色把你提供的代码高亮显示。这是一个非常有用的函数,它能返回指定的文件,并按照语法高亮突出显示文件内容。利用这个函数,用户可以拒绝浏览器端终止执行脚本的请求。 php_check_syntax 这个函数可以用来检查特定文件中的PHP语法是否正确。 highlight_string 当你想要把PHP代码显...

    wangshijun 评论0 收藏0
  • PHP常用180函数总结

    摘要:输入两个数组输出返回完成后的数组在数组中根据条件取出一段值,并返回。如果要再用遍历数组,必须使用。返回值数组中当前指针位置的键值对并向前移动数组指针。键值对被返回为四个单元的数组,键名为,,和。 数学函数 1.abs(): 求绝对值 $abs = abs(-4.2); //4.2 输入: 数字 输出: 绝对值数字 2.ceil(): 进一法取整 echo ceil(9.999); /...

    HtmlCssJs 评论0 收藏0
  • PHP基础知识小结

    原始数据类型(9种) 基本数据类型: 整形(integer)、浮点型(float)、字符串(string)、布尔型(boolean) 复合数据类型:数组(array)、对象(object)、callable(可调用) 特殊数据类型:资源类型(resource) 和 NULL 变量相关处理函数 is_bool($var)        判断是否为布尔型 is_int($var)    ...

    RancherLabs 评论0 收藏0

发表评论

0条评论

liukai90

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<