正则纠结中……
[^\f\n\r\t\v] <=> \S ,是否真的等同?
去掉[^\f\n\r\t\v],$s2中的空格去不了,加上以后,什么都不匹配……
function checkString($str){ //去掉非英文以外的其他语言包括乱码、空格。需要英文加特殊符号的内容 if(preg_match("/^ [ [:alnum:] [:space:] [:punct:] [^\f\n\r\t\v]]+ $/x",$str)){ return $str; }else{ echo "含非匹配项"; }}$s1 = "!!!!!!!";$s2 = "1 1 1 1 1 1 1";$s3 = "123asd";echo checkString($s1)."<br>";echo checkString($s2)."<br>";echo checkString($s3)."<br>";/*输出含非匹配项含非匹配项含非匹配项*/
if(preg_match("/^( [ [:alnum:] [:space:] [:punct:]]+ )$/x","11111111",$res)){ print_r($res); }else{ echo "含非匹配项"; //输出在这里}
if(preg_match("/^( [^\s]+ )/x","1 1 1 11111",$res)){ print_r($res); }else{ echo "含非匹配项"; }
[解决办法]
你按space键位的那个空格你都没包含进来,恰巧你的串里的空格是space那个,有点晕了吧,呵呵。
ascii码
--------------
space -> 32
\n -> 10
\t -> 9
\f -> 12
\r -> 13
\v -> 11
[解决办法]
你加x修正符然后正则里还有空格符,这不是白用x了么?
if(preg_match("/([^ \f\n\r\t\v]+)/","1 2 3 4444",$res)){ print_r($res)}
[解决办法]
第一个问题就说你想留下什么吧
第二个:
if(preg_match('/(.)\1\1/',$str,$matches)) echo '不能有连续三位相同';
[解决办法]
$s = "1 2 3 4444";//过滤空格的正则echo preg_replace('/\s+/', '', $s);//1234444//过滤重复字符的正则echo preg_replace('/(.)\\1+/', '$1', $s);//1 2 3 4//和在一起echo preg_replace(array('/(\s+)/', '/(.)\\1+/'), array('', '$1'), $s);//1234
[解决办法]
preg_match_all("/([\s*@#_)-])|((.)\\3+)/", $psw, $r);
print_r($r);
Array
(
[0] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => 11
[5] =>
[6] => 11
[7] =>
[8] => 11
[9] =>
[10] => *
[11] => @
[12] =>
[13] => #
[14] => _
[15] =>
[16] => )
[17] =>
[18] => -
)
[1] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] => *
[11] => @
[12] =>
[13] => #
[14] => _
[15] =>
[16] => )
[17] =>
[18] => -
)
[2] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => 11
[5] =>
[6] => 11
[7] =>
[8] => 11
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
)
[3] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => 1
[5] =>
[6] => 1
[7] =>
[8] => 1
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
)
)
[解决办法]
<?php$psw = "a 2 sw\fd 11\t11 11 *@\n#4_\r) \v-+?~'";preg_match_all('/([\s[:punct:]]|((.)\3))/',$psw,$matches);echo str_replace($matches[0],$matches[2],$psw),'<br>';$condition=array( "\t"=>'含制表符:\t', "\f"=>'含换页符:\f', "\r"=>'含回车符:\r', "\n"=>'含换行符:\n', "\v"=>'含垂直制表符:\v',);foreach ($matches[0] as $v){ if (in_array($v,array_keys($condition))) echo $condition[$v],'<br>'; if (preg_match('/([[:punct:]])/',$v)) echo '含特殊字符',$v,'<br>'; if (preg_match('/(.)\1/',$v)) echo '含重复字符',$v,'<br>';}/*a2swd1111114 含换页符:\f含重复字符11含制表符:\t含重复字符11含重复字符11含特殊字符*含特殊字符@含换行符:\n含特殊字符#含特殊字符_含回车符:\r含特殊字符)含特殊字符-含特殊字符+含特殊字符?含特殊字符~含特殊字符'*/