Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用preg_match检查字符串是否包含空格?_Php_Regex - Fatal编程技术网

Php 如何使用preg_match检查字符串是否包含空格?

Php 如何使用preg_match检查字符串是否包含空格?,php,regex,Php,Regex,我在php中使用此代码来检测字符串是否包含a-z、a-z、0-9以外的字符。我想检查字符串是否包含空格。我应该在模式中添加什么 if (preg_match ('/[^a-zA-Z0-9]/i', $getmail)) { // The string contains characters other than a-z and A-Z and 0-9 } 注意:我也从character类中删除了A-Z,因为您启用了大小写不敏感功能。如果要向当前图案中添加空间,您实际上需要在图案中输入空

我在php中使用此代码来检测字符串是否包含a-z、a-z、0-9以外的字符。我想检查字符串是否包含空格。我应该在模式中添加什么

if (preg_match ('/[^a-zA-Z0-9]/i', $getmail)) {
    // The string contains characters other than a-z and A-Z and 0-9
}

注意:我也从character类中删除了A-Z,因为您启用了大小写不敏感功能。

如果要向当前图案中添加空间,您实际上需要在图案中输入空间:

/[^a-z0-9 ]/i
         ^
      Notice the space there
    • 你可以试试这个

      /[^a-z0-9\s]/i
      
      使用
      i
      修饰符时,不需要
      A-Z
      \s
      部分将表示空格,但也表示制表符或任何空白

      /[^a-z0-9\s]/i