Masking Chinese Mobile Numbers in PHP Using substr_replace, preg_replace, and substr
This tutorial explains how to conceal 11‑digit Chinese mobile numbers in PHP by demonstrating three approaches: using substr_replace, applying a regular‑expression with preg_replace, and concatenating substr results, each illustrated with complete code examples.
The article addresses the common need to hide personal phone numbers in applications and provides three PHP‑based techniques for masking an 11‑digit Chinese mobile number.
Method 1: Using substr_replace
# substr_replace — Replace part of a string
# Usage
substr_replace ( mixed $string , mixed $replacement , mixed $start , mixed $length = ? ) : mixed
# $string: original string
# $replacement: replacement string
# $start: start position (negative values count from the end)
# $length: length to replace (negative values also count from $start)
# Example
$mobile = '18512341234';
echo substr_replace($mobile, '****', 3, 4); // 185****1234
echo substr_replace($mobile, '****', -8, -4); // 185****1234Method 2: Using regular expressions with preg_replace
# preg_replace — Perform a regular expression search and replace
# Usage
preg_replace ( mixed $pattern , mixed $replacement , mixed $subject , int $limit = -1 , int &$count = ? ) : mixed
# Example
$pattern = '/(\d{3})\d{4}(\d{4})/';
$new_mobile = preg_replace($pattern, '$1****$2', $mobile);
echo $new_mobile; // 185****1234Method 3: Using substr to concatenate parts
# substr — Return part of a string
# Usage
substr ( string $string , int $start , int $length = ? ) : string
# Example
echo substr($mobile, 0, 3) . '****' . substr($mobile, 7, 4); // 185****1234
echo substr($mobile, 0, 3) . '****' . substr($mobile, -4, 4); // 185****1234For further details, readers are encouraged to click the “Read Original” link to view the full article online.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
