当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > str_word_count() 函数

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 str_word_count() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 505 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The str_word_count() function counts the number of words in a string.
str_word_count()函数的作用是:计算字符串中的词数。

Syntax
语法

str_word_count(string,return,char)

Parameter参数 Description描述
string Required. Specifies the string to check
必要参数。指定字符串对象
return Optional. Specifies the return value of the str_word_count() function.
可选参数。指定str_word_count()函数需要返回的值。

Possible values:
可能值:

  • 0 - Default. Returns the number of words found
    0 – 默认值。返回找到的词的数量
  • 1 - Returns an array with the words from the string
    1 – 以数组的形式返回字符串中的词
  • 2 - Returns an array where the key is the position of the word in the string, and value is the actual word
    2 – 返回一个带有键和词所组成的组合数组。其中的键是词位于字符串中的位置;值是词本身
char Optional. Specifies special characters to be considered as words.
可选参数。指定组成词的特殊字符

Note: This parameter was added in PHP 5.1
注意:这个参数仅在PHP 5.1以上的版本中支持



Example 1
案例1

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

The output of the code above will be:
上述代码将输出下面的结果:

2


Example 2
案例2

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

The output of the code above will be:
上述代码将输出下面的结果:

Array
(
[0] => Hello
[1] => world
)


Example 3
案例3

<?php
print_r(str_word_count("Hello world!",2));
?>

The output of the code above will be:
上述代码将输出下面的结果:

Array
(
[0] => Hello
[6] => world
)


Example 4
案例4

str_word_count() without and with the char parameter:
带有 / 不带有char参数的str_word_count()函数举例:

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

The output of the code above will be:
上述代码将输出下面的结果:

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

评论 (0) All

登陆 | 还没注册?