【问题标题】:preg_match special characterspreg_match 特殊字符
【发布时间】:2010-10-14 21:04:58
【问题描述】:

如何使用preg_match查看字符串中是否存在特殊字符[^'£$%^&*()}{@:'#~?><>,;@|\-=-_+-¬`]

【问题讨论】:

  • 定义“特殊字符”...
  • [^'£$%^&*()}{@:'#~?>,;@|\-=-_+-¬`]
  • 为什么要使用preg_match()?如果您所做的只是检查这些字符是否存在,为什么不使用strpos()
  • 如果您输入的是 ASCII 字符,那么您可以只检查字符整数范围。

标签: php


【解决方案1】:

[\W]+ 将匹配任何非单词字符。

但要仅匹配问题中的字符,请使用:

  $string="sadw$"
  if(preg_match("/[\[^\'£$%^&*()}{@:\'#~?><>,;@\|\\\-=\-_+\-¬\`\]]/", $string)){
   //this string contain atleast one of these [^'£$%^&*()}{@:'#~?><>,;@|\-=-_+-¬`] characters
  }

【讨论】:

  • 感谢您的回答,但我只需要特定字符,例如 ^'£$%^&*()}{@:'#~?>,;@|\-=-_ +-¬`
  • 通常我会说[\W] 仍然适合您,因为“单词字符”指的是任何字母、数字或下划线,几乎不包括其他所有内容。我不确定它是否包含连字符。然后我注意到下划线在您要检查的字符列表中。由于您只想查找单个字符,因此使用explode()in_array() 而不是preg_match() 可能会更快,或者只使用while() 循环。虽然这些都不是很直观。
  • 如果字符串包含Unicode字符怎么办?您的正则表达式将它们排除在外!
  • [\W] 包括#
  • @d.raev 期望它包含 # 作为非单词字符
【解决方案2】:

使用preg_match。该函数接受正则表达式(模式)和主题字符串,如果匹配则返回1,如果不匹配则返回0,如果出现错误则返回false

$input = 'foo';
$pattern = '/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/';

if (preg_match($pattern, $input)){
    // one or more matches occurred, i.e. a special character exists in $input
}

您还可以为执行正则表达式匹配函数指定标志和偏移量。请参阅上面的文档链接。

【讨论】:

  • 个别符号有参考/链接吗?
【解决方案3】:

我的功能让生活更轻松。

function has_specchar($x,$excludes=array()){
    if (is_array($excludes)&&!empty($excludes)) {
        foreach ($excludes as $exclude) {
            $x=str_replace($exclude,'',$x);        
        }    
    }    
    if (preg_match('/[^a-z0-9 ]+/i',$x)) {
        return true;        
    }
    return false;
}

第二个参数 ($excludes) 可以传递您希望忽略的值。

用法

$string = 'testing_123';
if (has_specchar($string)) { 
    // special characters found
} 

$string = 'testing_123';
$excludes = array('_');
if (has_specchar($string,$excludes)) { } // false

【讨论】:

    【解决方案4】:

    对我来说,这是最好的:

    $string = 'Test String';
    
    $blacklistChars = '"%\'*;<>?^`{|}~/\\#=&';
    
    $pattern = preg_quote($blacklistChars, '/');
    if (preg_match('/[' . $pattern . ']/', $string)) {
       // string contains one or more of the characters in var $blacklistChars
    }
    

    【讨论】:

      【解决方案5】:

      您可以使用preg_quote 转义字符以在正则表达式中使用:

      preg_match('/' . preg_quote("[^'£$%^&*()}{@:'#~?><>,;@|\-=-_+-¬`]", '/') . '/', $string);
      

      http://php.net/manual/en/function.preg-quote.php

      【讨论】:

        【解决方案6】:

        这适用于所有 PHP 版本。结果是一个布尔值,需要相应地使用。

        要检查 id 字符串是否包含字符,您可以使用:

        preg_match( '/[a-zA-Z]/', $string );

        要检查字符串是否包含数字,您可以使用它。

        preg_match( '/\d/', $string );

        现在要检查一个字符串是否包含特殊字符,应该使用这个。

        preg_match('/[^a-zA-Z\d]/', $string);

        【讨论】:

          【解决方案7】:

          如果你想匹配特殊字符

          preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/', $input)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-12-07
            • 1970-01-01
            相关资源
            最近更新 更多