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.

php Courses
php Courses
php Courses
Masking Chinese Mobile Numbers in PHP Using substr_replace, preg_replace, and substr

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****1234

Method 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****1234

Method 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****1234

For further details, readers are encouraged to click the “Read Original” link to view the full article online.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPString FunctionsPhone Number Masking
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.