Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP – นับจำนวน Substring ด้วย mb_substr_count()

ฟังก์ชัน mb_substr_count() ใช้นับจำนวนครั้งที่ substring ปรากฏในสตริงที่กำหนด โดยรองรับ multibyte character

รูปแบบการใช้งาน (Syntax)

int mb_substr_count(string $haystack, string $needle, string $encoding = null)

พารามิเตอร์

  • $haystack – สตริงหลักที่ต้องการค้นหา
  • $needle – substring ที่ต้องการนับ
  • $encoding – character encoding หากละเว้นหรือ null จะใช้ internal character encoding

ค่าที่คืนกลับ

คืนจำนวนครั้งที่ needle substring ปรากฏใน haystack string

ตัวอย่าง

<?php
// นับจำนวน "is" ใน "This is a test"
$string = mb_substr_count("This is a test", "is");
echo $string;
?>

ผลลัพธ์

2