Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.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_匹配_Php_Regex_Preg Match - Fatal编程技术网

在php中使用空格、下划线、破折号和点进行preg_匹配

在php中使用空格、下划线、破折号和点进行preg_匹配,php,regex,preg-match,Php,Regex,Preg Match,我正在尝试使用以下代码匹配PHP中的正则表达式 if (!preg_match("/^[a-zA-Z0-9-_.]*$/",$string)) { // Show error } 我想允许 整数(0-9) 字符(小写字母和大写字母)(a到z和a到z) 点(.) 下划线(41; 破折号(-) 空格() 它不起作用。有人对我做错了什么有什么建议吗?你说得对 if (preg_match('/^[a-zA-Z0-9-_. ]*$/', $string) !== 1) { // Sho

我正在尝试使用以下代码匹配PHP中的正则表达式

if (!preg_match("/^[a-zA-Z0-9-_.]*$/",$string)) { 
  // Show error
 }
我想允许

  • 整数(0-9)
  • 字符(小写字母和大写字母)(a到z和a到z)
  • 点(.)
  • 下划线(41;
  • 破折号(-)
  • 空格()

它不起作用。有人对我做错了什么有什么建议吗?

你说得对

if (preg_match('/^[a-zA-Z0-9-_. ]*$/', $string) !== 1) {
  // Show error
}
假设您希望允许空字符串。否则,请将
*
更改为
+

您应该使用:

if (!preg_match("/^[\w\s\.-]*$/",$string)) {
    #show error
}

由于“*”的原因,它还将匹配空字符串。

这是一个非常肮脏和糟糕的解决方案,它允许任何东西,使用正则表达式有什么意义?