php 字符串里替换所有的邮箱用*号替换

     $oldStr = '[email protected]';
     $preg = "/([a-z0-9\-_\.]+@[a-z0-9]+\.[a-z0-9\-_\.]+)+/i";
     if (preg_match_all($preg, $oldStr , $matches)) {
         foreach ($matches[0] as $key=>$val){
             $temp_str = $val;
             $email_array = explode("@", $temp_str);
             if(strlen($email_array[0]) < 6){
                 $pre = '';
                 $star_len = strlen($email_array[0]);
                 if(strlen($email_array[0]) == 5){
                     $pre = substr( $email_array[0], 0, 1 );
                     $star_len = strlen($email_array[0])-1;
                 }
                 $star = '';
                 for($i = 0;$i < $star_len;$i ++){
                     $star .='*';
                 }
                 $temp_str = $pre.$star.'@'.$email_array[1];
             }else{
                 $pattern = '/([a-z0-9\-_\.])[a-z0-9\-_\.]{4}(([a-z0-9\-_\.])@[a-z0-9]+\.[a-z0-9\-_\.]+)/';
                 $replacement = "\$1****\$2";
                 $temp_str = preg_replace($pattern, $replacement, $temp_str);
             }
             $oldStr =  str_replace($val,$temp_str,$oldStr);
         }
     }

 

你可能感兴趣的:(php)