PHP学院的中学生 2019-03-15 14:38:17 1539次浏览 0条回复 0 0 0
/**
 * 所有直接在内容里面放入“微信”、“qq”、“手机”以及长串数字全部用*代替
 */
public static function replaceContact($str)
{
    $find = array(
        "微信",
        "qq",
        'QQ',
        '手机'
    );

    $replace = array(
        '**',
        '**',
        '**',
        '**'
    );
    $str = str_replace($find, $replace, $str);
    $pattern = '/[0123456789]{1}\d{5}\d*/';
    preg_match_all($pattern, $str, $matches, PREG_OFFSET_CAPTURE);
    if (!empty($matches[0][0])) {
        $len = strlen($str);
        foreach ($matches[0] as $k => $v) {
            $sub = strlen($str) - $len; // 最新长度和初始长度比较
            $a_str = '******'; // 替换字符串长度
            $start = $v[1] + $sub; // 出现的位置
            $lenth = strlen($v[0]);
            $str = substr_replace($str, $a_str, $start, $lenth);
        }
    }
    return $str;
}
    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册