Home  >  Article  >  Backend Development  >  Detailed explanation of the definition and usage examples of the statistical word count function implemented in PHP

Detailed explanation of the definition and usage examples of the statistical word count function implemented in PHP

巴扎黑
巴扎黑Original
2017-09-08 15:04:241199browse

This article mainly introduces the definition and usage of the statistical word count function implemented in PHP, and analyzes PHP's statistical operation skills for strings in the form of examples. Friends in need can refer to the examples in this article

Describes the definition and use of the statistical word count function implemented in PHP. Share it with everyone for your reference, the details are as follows:


<?php
//函数定义:
function countWords($str){
  echo (mb_strlen($str, &#39;utf8&#39;) + strlen($str))/2;
}
//使用方法:
$strdemo1=&#39;欢迎访问脚本之家&#39;;
$strdemo2=&#39;http://www.jb51.net&#39;;
$strdemo3=&#39;欢迎访问脚本之家http://www.jb51.net&#39;;
countWords($strdemo1);
echo "<br/>";
countWords($strdemo2);
echo "<br/>";
countWords($strdemo3);
?>

Running results:


16
19
35

The above is the detailed content of Detailed explanation of the definition and usage examples of the statistical word count function implemented in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn