Để kiểm tra chuỗi có khoảng trắng hay không, dùng preg_match() trong PHP.
Cú pháp:
preg_match('/\s/', $yourVariableName);
Ví dụ
<!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>
Kết quả
The name (John Smith) has the space