PHP str_word_count()ฟังก์ชั่น
ตัวอย่าง
นับจำนวนคำที่พบในสตริง "Hello World!":
<?php
echo str_word_count("Hello world!");
?>
ความหมายและการใช้งาน
ฟังก์ชัน str_word_count() นับจำนวนคำในสตริง
ไวยากรณ์
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. Possible values:
|
char | Optional. Specifies special characters to be considered as words. |
รายละเอียดทางเทคนิค
คืนมูลค่า: | ส่งกลับตัวเลขหรืออาร์เรย์ ขึ้นอยู่กับพารามิเตอร์ การ ส่งคืน ที่เลือก |
---|---|
รุ่น PHP: | 4.3.0+ |
บันทึกการเปลี่ยนแปลง: | เพิ่ม พารามิเตอร์ charใน PHP 5.1 |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
ส่งคืนอาร์เรย์ด้วยคำจากสตริง:
<?php
print_r(str_word_count("Hello world!",1));
?>
ตัวอย่าง
ส่งกลับอาร์เรย์ที่คีย์คือตำแหน่งของคำในสตริง และค่าคือคำจริง:
<?php
print_r(str_word_count("Hello world!",2));
?>
ตัวอย่าง
ไม่มีและด้วยพารามิเตอร์ char:
<?php
print_r(str_word_count("Hello world & good morning!",1));
print_r(str_word_count("Hello world & good morning!",1,"&"));
?>
❮ การอ้างอิงสตริง PHP