Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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中检查字符串是否包含像{和}这样的特殊字符_Php_String_Preg Match_Character - Fatal编程技术网

如何在php中检查字符串是否包含像{和}这样的特殊字符

如何在php中检查字符串是否包含像{和}这样的特殊字符,php,string,preg-match,character,Php,String,Preg Match,Character,在这个输入字段中,我希望用户不能使用以下特殊标记:{}[]$ 现在我的代码中有以下解决方案,但问题是它不允许ä、ö、ü或其他类似的字符 if (preg_match('/^[-a-zA-Z0-9 .]+$/', $string) || empty($string)){ echo "Everything ok!"; else{ echo "Everything not ok!";} 因此,我尝试使用preg_match('/^[\p{L}\p{N}.-]+$/',$string),因为据说它

在这个输入字段中,我希望用户不能使用以下特殊标记:{}[]$

现在我的代码中有以下解决方案,但问题是它不允许ä、ö、ü或其他类似的字符

if (preg_match('/^[-a-zA-Z0-9 .]+$/', $string)  || empty($string)){
echo "Everything ok!";
else{ 
echo "Everything not ok!";}
因此,我尝试使用preg_match('/^[\p{L}\p{N}.-]+$/',$string),因为据说它允许来自任何语言的字符,但该解决方案不允许像@和*(我认为可能需要)这样的标记。那么,有什么解决方案允许除了{}[]$-标记之外的任何东西吗?非常感谢您的帮助,因为我不知道该写些什么来让它工作。

我就是这样做的:

if (preg_match('/[^a-zA-Z\d]/', $string)) {
//$string contains special characters, do something.
}

为什么不将允许的字符添加到character类中呢?与其定义“除了
{}[]$
之外的任何内容”=
[^{}[\]$]
(用
^
+$
来测试整个输入),不如定义允许的内容。听起来没那么难。@Jon:谢谢你指出我以前解决方案中的拼写错误。我今天早些时候试过,但似乎完全忘记了马克。难怪我第一次尝试时就出错了。真的非常感谢,为我的错误致歉。