確保字串沒有空格?


若要檢查字串中是否有空格,請在 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

更新於: 13-Oct-2020

3K+ 瀏覽量

開啟您的 職業 生涯

完成課程以獲得認證

開始學習
廣告
© . All rights reserved.