日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

PHP | 计算字符串中的单词总数

發布時間:2025/3/11 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP | 计算字符串中的单词总数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Given a string and we have to count the total number of words in it.

給定一個字符串,我們必須計算其中的單詞總數。

str_word_count() function

str_word_count()函數

To find the total number of words in a string, we can use str_word_count() function – which is a library function in PHP – it returns the total number of words in the string.

要查找字符串中的單詞總數 ,我們可以使用str_word_count()函數(這是PHP中的庫函數),它返回字符串中的單詞總數。

Syntax:

句法:

str_word_count(string,return,char);

Here,

這里,

  • string is a required parameter, this is the string in which we have to find the total number of words.

    string是必需的參數,這是我們必須在其中查找單詞總數的字符串。

  • return – it is an optional parameter used for specifying the return type – it can accept three values

    return –它是用于指定返回類型的可選參數–它可以接受三個值

    • 0 – Which is the default value, it specifies the return the number of words in the string.
    • 0 –這是默認值,它指定返回字符串中的單詞數。
    • 1 – It specifies the return of the array with the words.
    • 1 –它指定帶有單詞的數組的返回。
    • 2 – It specifies the return of the array where key is the position and value is the actual word.
    • 2 –它指定數組的返回值,其中key是位置,而value是實際單詞。
  • char – it is an optional parameter – it is used to specify a special character to consider as a word.

    char –這是一個可選參數–用于指定要視為單詞的特殊字符。

PHP code to count the total number of words in a string

PHP代碼計算字符串中單詞的總數

<?php //input string $str = "The Quick brown fox jumps right over The Lazy Dog"; //counting the words by calling str_word_count function $response = str_word_count($str); //printing the result echo "There are ".$response." Words in <b>".$str."</b>".'<br/>';//input string $str = "Hello @ 123 . com"; //counting the words by calling str_word_count function $response = str_word_count($str); //printing the result echo "There are ".$response." Words in <b>".$str."</b>".'<br/>';//input string $str = "Hello @ IncludeHelp @ com"; //counting the words by calling str_word_count function //specify the '@' as a word $response = str_word_count($str, 0, '@'); //printing the result echo "There are ".$response." Words in <b>".$str."</b>".'<br/>'; ?>

Output

輸出量

There are 10 Words in The Quick brown fox jumps right over The Lazy Dog There are 2 Words in Hello @ 123 . com There are 5 Words in Hello @ IncludeHelp @ com

Explanation:

說明:

In PHP, We have the function str_word_count() to count the number of words in a string. We use the same to get the number of words in the string ($str) and store the output of the function in ($response) then use echo to print the result.

在PHP中,我們具有函數str_word_count()來計算字符串中的單詞數。 我們使用相同的方法獲取字符串( $ str )中的單詞數,并將函數的輸出存儲在( $ response )中,然后使用echo打印結果。

翻譯自: https://www.includehelp.com/php/count-the-total-number-of-words-in-a-string.aspx

總結

以上是生活随笔為你收集整理的PHP | 计算字符串中的单词总数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。