หากต้องการตรวจสอบว่าสตริงไม่มีช่องว่างหรือไม่ ให้ใช้ preg_match() ใน PHP
ไวยากรณ์มีดังนี้
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