PHP String str_word_count() 函數
str_word_count() 是 PHP 的內置函數。愛掏網 - it200.com它用于返回字符串中使用的單詞的信息或計算字符串中單詞的數量。愛掏網 - it200.com
str_word_count(string,return,char)
參數 | 描述 | 必需/可選 |
---|---|---|
string | 指定要檢查的字符串。愛掏網 - it200.com | 必需。愛掏網 - it200.com |
Char | 指定字符 | 可選。愛掏網 - it200.com |
return | 指定函數的返回值。愛掏網 - it200.com 值: | 可選 |
- 0:默認值,返回找到的單詞數。愛掏網 - it200.com
- 1:返回包含單詞的數組。愛掏網 - it200.com
- 2:返回實際單詞的值。愛掏網 - it200.com
示例1
<?php
str="PHP Javatpoint";
echo "Your string is:".str;
echo "<br>";
echo "By using str_word_count(): ".str_word_count($str);
?>
輸出:
Your string is:PHP Javatpoint
By using str_word_count(): 2
示例2
<?php
str="PHP Javatpoint";
echo "Your string is:".str;
echo "<br>";
print_r(str_word_count("$str",1));
?>
輸出:
Your string is:PHP Javatpoint
Array (
[0] => PHP
[1] => Javatpoint
)
示例3
<?php
print_r(str_word_count("Hello PHP & Javatpoint!",1));
echo "<br>";
print_r(str_word_count("Hello PHP & Javatpoint!",1,"&"));
?>
輸出:
Array ( [0] => Hello [1] => PHP [2] => Javatpoint ) Array ( [0] => Hello [1] => PHP [2] => & [3] => Javatpoint )
聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。