Home > Backend Development > PHP Tutorial > PHP word count function definition and usage

PHP word count function definition and usage

小云云
Release: 2023-03-19 19:50:01
Original
1777 people have browsed it

This article mainly introduces the definition and usage of the statistical word count function implemented in PHP, and analyzes the operating techniques related to PHP's statistical operations for strings in the form of examples. Friends in need can refer to it, and I hope it can help everyone.


<?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);
?>
Copy after login

Run results:


##

16
19
35
Copy after login
Related recommendations:


Detailed explanation of javascript callback function definition and usage examples

Detailed explanation of javascript function definition, calling mode and initialization example of this

php custom function Detailed explanation of definitions and syntax examples

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template