php手机号中间几位替换星号方法总结

1、字符串中包含多个手机号码

<?php
$s='王经理:13999312365 李经理:13588958741';
$s=preg_replace('#(\d{3})\d{5}(\d{3})#', '${1}*****${2}', $s);
echo $s;
//王经理:139*****365 李经理:135*****741
?>

2、字符串中只有一个手机号码

<?php
$haoma="15012345678";
echo preg_replace("/(\d{3})\d{5}/","$1*****",$haoma);
//150*****678
?>

不用正则表达式实现
1、使用substr_replace字符串部分替换函数

<?php
$string1="13264309555";
echo substr_replace($string1,'*****',3,5);
//132*****555
?>

2、使用字符串截取函数substr

<?php
echo substr($string1,0,3)."*****".substr($string1,8,3);
//132*****555
?>

Mobile Device Detection Framework (PHP)

//initialize all known devices as false
$iPod = false;
$iPhone = false;
$iPad = false;
$iOS = false;
$webOSPhone = false;
$webOSTablet = false;
$webOS = false;
$BlackBerry9down = false;
$BlackBerry10 = false;
$RimTablet = false;
$BlackBerry = false;
$NokiaSymbian = false;
$Symbian = false;
$AndroidTablet = false;
$AndroidPhone = false;
$Android = false;
$WindowsPhone = false;
$WindowsTablet = false;
$Windows = false;
$Tablet = false;
$Phone = false;

//Detect special conditions devices & types (tablet/phone form factor)
if(stripos($_SERVER['HTTP_USER_AGENT'],"iPod")){
    $iPod = true;
    $Phone = true;
    $iOS = true;
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"iPhone")){
    $iPhone = true;
    $Phone = true;
    $iOS = true;
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"iPad")){
    $iPad = true;
    $Tablet = true;
    $iOS = true;
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"webOS")){

    $webOS = true;

    if(stripos($_SERVER['HTTP_USER_AGENT'],"Pre") || stripos($_SERVER['HTTP_USER_AGENT'],"Pixi")){
        $webOSPhone = true;
        $Phone = true;
    }
    if(stripos($_SERVER['HTTP_USER_AGENT'],"TouchPad")){
        $webOSTablet = true;
        $Tablet = true;
    }
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"BlackBerry")){
    $BlackBerry = true;
    $BlackBerry9down = true;
    $Phone = true;
{
if(stripos($_SERVER['HTTP_USER_AGENT'],"BB10")){
    $BlackBerry = true;
    $BlackBerry10 = true;
    $Phone = true;
{
if(stripos($_SERVER['HTTP_USER_AGENT'],"RIM Tablet")){
    $BlackBerry = true;
    $RimTablet = true;
    $Tablet = true;
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"SymbianOS")){
    $Symbian = true;
    $NokiaSymbian = true;
    $Phone = true;
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"Android")){

    $Android = true;

    if(stripos($_SERVER['HTTP_USER_AGENT'],"mobile")){
        $AndroidPhone = true;
        $Phone = true;
    }else{
        $AndroidTablet = true;
        $Tablet = true;
    }
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"Windows")){

    $Windows = true;

    if(stripos($_SERVER['HTTP_USER_AGENT'],"Touch")){
        $WindowsTablet = true;
        $Tablet = true;
    }
    if(stripos($_SERVER['HTTP_USER_AGENT'],"Windows Phone")){
        $WindowsPhone = true;
        $Phone = true;
    }
}



//Target form factors
if( $Phone ){
    //we're phone form factor
}else if( $Tablet ){
    //we're a tablet form factor
}else{
    //we're neither a phone or tablet
}




//Target operating systems
if( $iOS ){
    //we're an iOS device
}else if( $Android ){
    //we're an Android device
}else if( $Windows ){
    //we're a Windows device
}else if( $BlackBerry ){
    //we're a BlackBerry device
}else if( $webOS ){
    //we're a webOS device
}else if( $Symbian ){
    //we're a Symbian device
}else{
    //we're neither a phone or tablet
}




//Target individual devices
if( $iPod || $iPhone ){
    //we're an iPhone/iPod touch -- do something here
}else if($iPad){
    //we're an iPad -- do something here
}else if($AndroidPhone){
    //we're an Android Phone -- do something here
}else if($AndroidTablet){
    //we're an Android Tablet -- do something here
}else if($WindowsPhone){
    //we're an Windows Phone -- do something here
}else if($WindowsTablet){
    //we're an Windows Tablet -- do something here
}else if($webOSPhone){
    //we're a webOS phone -- do something here
}else if($webOSTablet){
    //we're a webOS tablet -- do something here
}else if($BlackBerry9down){
    //we're an outdated BlackBerry phone -- do something here
}else if($BB10){
    //we're an new BlackBerry phone -- do something here
}else if($RimTablet){
    //we're a RIM/BlackBerry Tablet -- do something here
}else if($NokiaSymbian){
    //we're a Nokia Symbian device -- do something here
}else{
    //we're not a known device.
}

经典PHP加密解密函数Authcode()修复版代码

 Authcode这个函数很多人都使用,这函数来自Discuz程序,用于加密解密字符串,可以设置钥匙(key)和过期时间,在很多时候都用得着。原版的函数代码可能会生成+、/、&这样的字符,导致通过URL传值取回时被转义,导致无法解密。火端网络稍加修改,把这几个字符替换成其它字符,解密时再替换回去,这样就完美了!

function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0){
 
 if($operation == 'DECODE') {
  $string = str_replace('[a]','+',$string);
  $string = str_replace('[b]','&',$string);
  $string = str_replace('','/',$string);
 }
    $ckey_length = 4;
    $key = md5($key ? $key : 'livcmsencryption ');
    $keya = md5(substr($key, 0, 16));
    $keyb = md5(substr($key, 16, 16));
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
    $cryptkey = $keya.md5($keya.$keyc);
    $key_length = strlen($cryptkey);
    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
    $string_length = strlen($string);
    $result = '';
    $box = range(0, 255);
    $rndkey = array();
    for($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }
    for($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }
    for($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }
    if($operation == 'DECODE') {
        if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
   
            return substr($result, 26);
        } else {
            return '';
        }
    } else {
  $ustr = $keyc.str_replace('=', '', base64_encode($result));
  $ustr = str_replace('+','[a]',$ustr);
  $ustr = str_replace('&','[b]',$ustr);
  $ustr = str_replace('/','',$ustr);
        return $ustr;
    }
}