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

ฟังก์ชัน ctype_space() ใน PHP

คำอธิบาย

ฟังก์ชัน ctype_space() ตรวจสอบว่าตัวอักษรทุกตัวในสตริงเป็นตัวอักษรช่องว่าง (whitespace) หรือไม่ รวมถึง space, tab, vertical tab, line feed, carriage return และ form feed

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

ctype_space(str)

พารามิเตอร์

  • str — สตริงที่ต้องการตรวจสอบ

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

คืนค่า TRUE หากทุกตัวอักษรเป็น whitespace หรือ FALSE หากไม่ใช่

ตัวอย่างการใช้งาน

<?php
$arr = array(' ', "\t\n\r", '\test123');
foreach ($arr as $demo) {
    if (ctype_space($demo)) {
        echo "$demo consists of all whitespace characters.\n";
    } else {
        echo "$demo does not have all whitespace characters.\n";
    }
}
?>

ผลลัพธ์

  consists of all whitespace characters.
	
 consists of all whitespace characters.
\test123 does not have all whitespace characters.

ตัวอย่างเพิ่มเติม

<?php
$str = " ";
if (ctype_space($str)) echo "Whitespace!!\n";
else echo "No Whitespace character!\n";
?>

ผลลัพธ์

Whitespace!!