Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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_String_Validation_Preg Match - Fatal编程技术网

Php preg_匹配验证字符串中的两个单词

Php preg_匹配验证字符串中的两个单词,php,string,validation,preg-match,Php,String,Validation,Preg Match,问题: if (!preg_match('^\w+\s\w+$', $_POST['freetext'])) { $error_array[] = 'missingwords'; } 尝试在提交前验证表单字段是否至少有两个单词 PHP代码: if (!preg_match('^\w+\s\w+$', $_POST['freetext'])) { $error_array[] = 'missingwords'; } 当前状态: if (!preg_match('^\w+\s\w

问题:

if (!preg_match('^\w+\s\w+$', $_POST['freetext']))
{
    $error_array[] = 'missingwords';
}
尝试在提交前验证表单字段是否至少有两个单词

PHP代码:

if (!preg_match('^\w+\s\w+$', $_POST['freetext']))
{
    $error_array[] = 'missingwords';
}
当前状态:

if (!preg_match('^\w+\s\w+$', $_POST['freetext']))
{
    $error_array[] = 'missingwords';
}
即使我输入了两个中间有空格的单词,也会打印错误。这里可能有什么问题?

PCRE regexen需要:

你需要打开错误报告,它会告诉你的

if (!preg_match('#^\w+\s\w+#', $_POST['freetext']))
{
    $error_array[] = 'missingwords';
}
两个问题:

  • 模式需要分隔符。我补充说#
  • 我删除了$mark,因为您至少需要两个单词。如果有$mark,则只有正好包含2个单词的字符串才能通过 试一试

    if(str\u单词计数($str)