日期:2014-05-17 浏览次数:20592 次
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] => *