Home  >  Article  >  Backend Development  >  Example of replacing asterisks in the middle of php mobile phone number_PHP tutorial

Example of replacing asterisks in the middle of php mobile phone number_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:50:15952browse

Today, the editor will introduce to you how to use the preg_replace function to replace the specified numbers in the middle of the mobile phone number with asterisks. This is used on many websites. Everyone will do this, let me introduce the example method below.


Regular expression method

1. The string contains multiple mobile phone numbers

The code is as follows
 代码如下 复制代码

$s='王经理:13999312365 李经理:13588958741';
$s=preg_replace('#(d{3})d{5}(d{3})#', '*****', $s);
echo $s;
//王经理:139*****365 李经理:135*****741
?>

Copy code

 代码如下 复制代码

$haoma="15012345678";
echo preg_replace("/(d{3})d{5}/","*****",$haoma);
//150*****678
?>

$s='Manager Wang: 13999312365 Manager Li: 13588958741';
$s=preg_replace('#(d{3})d{5}(d{3})#', '${1}*****${2}', $s);

echo $s;
代码如下 复制代码

$string1="13264309555";
echo substr_replace($string1,'*****',3,5);
//132*****555
?>

//Manager Wang: 139*****365 Manager Li: 135*****741

?>

 代码如下 复制代码

echo substr($string1,0,3)."*****".substr($string1,8,3);
//132*****555
?>

2. There is only one mobile phone number in the string
The code is as follows

Copy code
$haoma="15012345678"; echo preg_replace("/(d{3})d{5}/","$1*****",$haoma); //150*****678 ?> Achieved without regular expressions 1. Use substr_replace string partial replacement function
The code is as follows Copy code
$string1="13264309555"; <🎜> echo substr_replace($string1,'*****',3,5); <🎜> //132*****555 <🎜> ?> 2. Use the string interception function substr
The code is as follows Copy code
echo substr($string1,0,3)."*****".substr($string1,8,3); <🎜> //132*****555 <🎜> ?> http://www.bkjia.com/PHPjc/632653.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632653.htmlTechArticleToday, the editor will introduce to you how to use the preg_replace function to replace the specified numbers in the middle of the mobile phone number with asterisks. This is done on many websites. Let me introduce examples below...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn