推薦:《PHP視頻教程》
今天突然想起手機(jī)號(hào),這種隱私信息在項(xiàng)目中還是隱藏一下比較好,所以就來總結(jié)一下能用的方法,僅限于大陸的11位手機(jī)號(hào)
方法演示
1.使用 substr_replace
函數(shù)
# substr_replace — 替換字符串的子串 # 使用說明 substr_replace ( mixed $string , mixed $replacement , mixed $start , mixed $length = ? ) : mixed # $string 資源字符串 # $replacement 替換字符 # $start 替換開始位置,如果位負(fù)數(shù)的時(shí)候,將從末尾開始數(shù) # $length 需要替換的長(zhǎng)度,如果為負(fù)數(shù)的時(shí)候,也是從$start開始位置替換 # substr_replace() 在字符串 string 的副本中將由 start 和可選的 length 參數(shù)限定的子字符串使用 replacement 進(jìn)行替換。 # 示例 $mobile = '18512341234'; echo substr_replace($mobile, '****', 3, 4); // 185****1234 # 注意 字符串的開始位置為0 echo substr_replace($mobile, '****', -8, -4); // 185****1234
2.使用正則表達(dá)式
# preg_replace — 執(zhí)行一個(gè)正則表達(dá)式的搜索和替換 # 使用說明 preg_replace ( mixed $pattern , mixed $replacement , mixed $subject , int $limit = -1 , int &$count = ? ) : mixed # 搜索 subject 中匹配 pattern 的部分,以 replacement 進(jìn)行替換。 # 示例 $pattern = '/(d{3})d{4}(d{4})/'; $new_mobile = preg_replace($pattern, '$1****$2', $mobile); echo $new_mobile;
3.使用 substr
函數(shù)
# 函數(shù)說明 substr ( string $string , int $start , int $length = ? ) : string # 返回字符串 string 由 start 和 length 參數(shù)指定的子字符串。 # 同 substr_replace 一樣,start也可以為負(fù)數(shù)的 # 示例 echo substr($mobile, 0,3) . '****' . substr($mobile, 7,4); echo substr($mobile, 0,3) . '****' . substr($mobile, -4,4);
總結(jié)
方法有很多,多數(shù)情況是在情況選擇那種更加合適.