2018年12月

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>案例测试</title>
<link rel="stylesheet" href="css/all.css" />
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<style>
/* 公共样式 */
* { padding: 0; margin: 0; list-style: none; font-size: 14px; }
.hide { display: none; }
input { outline: none; }
/* 模拟下拉框 */
.select { position: relative; margin: 50px 0 0 100px; }
.select .input_in input { width: 188px; height: 20px; line-height: 20px; padding: 10px 0 10px 10px; border: 1px solid #d6d6d6; cursor: pointer; }
.select .city { position: absolute; top: 40px; left: 0; }
.select .city ul { width: 198px; border: 1px solid #d6d6d6; border-top: none; }
.select .city ul li { padding-left: 10px; width: 188px; height: 40px; line-height: 40px; cursor: pointer; }
</style>
</head>

<body>
<!-- End 模拟下拉框 -->
<div class="select">
    <div class="input_in">
        <input type="text" value="D.C" />
    </div>
    <div class="city hide">
        <ul>
            <li>New York1</li>
            <li>New York2</li>
            <li>New York3</li>
            <li>New York4</li>
            <li>New York5</li>
            <li>New York6</li>
        </ul>
    </div>
</div>
<!-- End 模拟下拉框 -->
<script type="text/javascript">
$(function(){
//模拟下拉框
    $('.select input').on('click',function(){
        if($('.select .city').is('.hide')){
            $('.select .city').removeClass('hide');
        }else{
            $('.select .city').addClass('hide');
        }
    })
    $('.select ul li').on('click',function(){
        $('.select input').val($(this).html());
        $('.select .city').addClass('hide');
        $('.select input').css('border-bottom','1px solid $d6d6d6');
    })
    $('.select ul li').hover(
        function(){
            $(this).css({'backgroundColor':'#fd9','font-size':'18px'});
        },function(){
            $(this).css({'backgroundColor':'#fff','font-size':'14px'});
        }
    )  
})
</script>
</body>
</html>

function hideStr($string, $bengin = 0, $len = 4, $type = 0, $glue = "@"){

if (empty($string))
    return false;
$array = array();
if ($type == 0 || $type == 1 || $type == 4) {
    $strlen = $length = mb_strlen($string);
    while ($strlen) {
        $array[] = mb_substr($string, 0, 1, "utf8");
        $string = mb_substr($string, 1, $strlen, "utf8");
        $strlen = mb_strlen($string);
    }
}
if ($type == 0) {
    for ($i = $bengin; $i < ($bengin + $len); $i++) {
        if (isset($array[$i]))
            $array[$i] = "*";
    }
    $string = implode("", $array);
} else if ($type == 1) {
    $array = array_reverse($array);
    for ($i = $bengin; $i < ($bengin + $len); $i++) {
        if (isset($array[$i]))
            $array[$i] = "*";
    }
    $string = implode("", array_reverse($array));
} else if ($type == 2) {
    $array = explode($glue, $string);
    $array[0] = hideStr($array[0], $bengin, $len, 1);
    $string = implode($glue, $array);
} else if ($type == 3) {
    $array = explode($glue, $string);
    $array[1] = hideStr($array[1], $bengin, $len, 0);
    $string = implode($glue, $array);
} else if ($type == 4) {
    $left = $bengin;
    $right = $len;
    $tem = array();
    for ($i = 0; $i < ($length - $right); $i++) {
        if (isset($array[$i]))
            $tem[] = $i >= $left ? "*" : $array[$i];
    }
    $array = array_chunk(array_reverse($array), $right);
    $array = array_reverse($array[0]);
    for ($i = 0; $i < $right; $i++) {
        $tem[] = $array[$i];
    }
    $string = implode("", $tem);
}
return $string;

}
$str = '12345678901';
echo hideStr($str,2,4);


将一个字符串部分字符用*替代隐藏

@param string $string 待转换的字符串
@param int $bengin 起始位置,从0开始计数,当$type=4时,表示左侧保留长度
@param int $len 需要转换成*的字符个数,当$type=4时,表示右侧保留长度
@param int $type 转换类型:0,从左向右隐藏;1,从右向左隐藏;2,从指定字符位置分割前由右向左隐藏;3,从指定字符位置分割后由左向右隐藏;4,保留首末指定字符串

@param string $glue 分割符

@return string 处理后的字符串