ในการค้นหาจำนวนอักขระในสตริง โค้ด PHP มีดังต่อไปนี้ -
ตัวอย่าง
<?php
$my_string = "Hi there, this is a sample ";
$len = mb_strlen($my_string);
print_r("The number of characters in the string is ");
echo $len;
?> ผลลัพธ์
The number of characters in the string is 27
ด้านบน มีการกำหนดสตริงที่มีช่องว่างมากกว่าที่กำหนดระหว่าง −
$my_string = "Hi there, this is a sample ";
จากนั้น คำนวณความยาวของสตริงโดยใช้ฟังก์ชัน 'strlen' ความยาวนี้ถูกกำหนดให้กับตัวแปร ซึ่งจะทำให้อักขระที่อยู่ในสตริงรวมถึงช่องว่างด้วย -
$len = mb_strlen($my_string);
จากนั้นพิมพ์ความยาวที่คำนวณได้บนหน้าจอ −
print_r("The number of characters in the string is ");
echo $len;