21,887
社区成员
发帖
与我相关
我的任务
分享
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 "含非匹配项"; //输出在这里
}
<?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
含特殊字符)
含特殊字符-
含特殊字符+
含特殊字符?
含特殊字符~
含特殊字符'
*/
([\s*@#_)-])|((.)\\3+)
$psw = "a 2 sw\fd 11\t11 11 *@\n#4_\r) \v-";
preg_match_all("/([\s*@#_)-])|((.)\\3+)/", $psw, $r);
//print_r($r);echo"<br>";
foreach($r as $s => $res){
foreach($res as $e => $rest){
if($rest == "\t") echo "{$s}-{$e}含制表符:\\t<br>";
if($rest == "\f") echo "{$s}-{$e}含换页符:\\f<br>";
if($rest == "\r") echo "{$s}-{$e}含回车符:\\r<br>";
if($rest == "\n") echo "{$s}-{$e}含换行符:\\n<br>";
if($rest == "\v") echo "{$s}-{$e}含垂直制表符:\\v<br>";
//这里怎么表示?if(preg_match_all('/(.)\1\1/',$rest,$matches)) echo "{$s}-{$e}含重复字符:{$matches[0]}<br>";
//这里怎么表示?if(preg_match_all('/(.)\1\1/',$rest,$matches)) echo "{$s}-{$e}含特殊字符:{$matches[0]}<br>";
$result[$s][] = htmlentities($rest);
}
}
print_r($result);
/* printout:
0-2含换页符:\f
0-5含制表符:\t
0-12含换行符:\n
0-15含回车符:\r
1-2含换页符:\f
1-5含制表符:\t
1-12含换行符:\n
1-15含回车符:\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] => ) )
preg_replace(array('/(\s+)/', '/(.)\\1+/'), array('', '$1'), $s);
$psw = "a 2 sw\fd 11\t11 11 *@\n#4_\r) \v-";
if(preg_match_all($pattern,$psw,$res)){
print_r($res); //获取到 a2swd1111114,这样能实现吗?,实在不行是获取a2swd14也行
return $res;
}else{
//将非匹配项罗列出来,添加提示
echo "包含重复字符:111111";
echo "包含特殊字符:*@#_)-";
echo "包含空格";
echo "包含制表符:\t";
echo "包含换页符:\f";
echo "包含回车符:\r";
echo "包含换行符:\n";
echo "包含垂直制表符:\v";
}
$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
if(preg_match_all("/([^\f\n\r\t\v][(.)\1\1]+)/","1 2 3 4444",$res))
{
print_r($res);
}else{
echo "含非匹配项"; //这里输出
}
if(preg_match('/(.)\1\1/',$str,$matches))
echo '不能有连续三位相同';
if(preg_match("/([^ \f\n\r\t\v]+)/","1 2 3 4444",$res))
{
print_r($res);
}
//输出
Array ( [0] => 1 [1] => 1 ) //2 3 4444呢?
if(preg_match("/([^ \f\n\r\t\v]+)/","1 2 3 4444",$res))
{
print_r($res)
}
if(preg_match("/^( [^\s]+ )/x","1 1 1 11111",$res)){
print_r($res);
}else{
echo "含非匹配项";
}
Array
(
[0] => 1
[1] => 1
)