PHP 确保字符串不包含空格?

phpserver side programmingprogramming更新于 2025/8/18 1:22:17

要检查字符串是否不包含空格,请使用 PHP 中的 preg_match()。

语法如下 

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

相关文章