需要过滤掉用户输入,仅字母数字、逗号、句号和空格


Need to filter out user input to only alphanumeric, commas, fullstop and spaces

我需要将用户输入过滤为仅字母数字,逗号,句号和空格。我实际上在下面尝试了这段代码,但其他不需要的字符正在经历。

if(!preg_match('/^[,. 0-9A-Za-z]/', $input)
{
   echo "invalid";
}

如何使输入严格接受上述字符?

否定字符 ( ^ ) 需要进入字符类的内部:

if(preg_match('/[^,'. 0-9A-Z]/i', $input))
{
   echo "invalid";
}