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

PHP ตรวจสอบว่าสตริงไม่มี whitespace

เพื่อตรวจสอบว่าสตริงไม่มี whitespace ใช้ preg_match() ใน PHP

Syntax ดังนี้

preg_match('/\s/',$yourVariableName);

ตัวอย่าง

โค้ด PHP ดังนี้

<!DOCTYPE html> <html> <body> <?php 
$name="John Smith"; 
if ( preg_match('/\s/',$name) ){ 
    echo "The name (",$name,") has the space"; 
} else { 
    echo "The Name (",$name,") has not the space"; 
} 
?> </body> </html>

ผลลัพธ์

นี่จะผลิตเอาต์พุตดังนี้

The name (John Smith) has the space