Replace PHP strings with asterisks or other characters (phone number, id) _php Tutorial

Source: Internet
Author: User
A character in a string specified in PHP is replaced with an asterisk we have a function that can be implemented such as Substr,preg_replace,substr_replace and so on, I give these three functions respectively to introduce an example, mainly talk about telephone, ID card.

Replace the phone number string with the asterisk code:

The code is as follows Copy Code

$str = "15832818835";
Echo substr ($str, 0, 3). ' '. substr ($str, 8,strlen ($STR));//reserved for the first three and last three bits
?>

or with regular

$s = ' manager Wang of www.bKjia.c0m: 13999312365 manager li: 13588958741 ';
$s =preg_replace (' # (D{3}) d{5} (D{3}) # ', ' ${1}*****${2} ', $s);
Echo $s;
Manager Wang: 139*****365 li: 135*****741
?>


Replace string middle position character obscured stars number

The code is as follows Copy Code

function Half_replace ($STR) {
$len = strlen ($STR)/2;
Return Substr_replace ($str, Str_repeat (' * ', $len), Ceil (($len)/2), $len);
}

echo half_replace (' Test '), "n", Half_replace (' tests '), "n", Half_replace (' exceptions ');

The PHP ID number is marked with an asterisk

The code is as follows Copy Code

echo strlen ($idcard) ==15?substr_replace ($idcard, "* * *", 8,4):(strlen ($idcard) ==18?substr_replace ($idcard, "* * *", 10,4): "bkjia.c0m hint ID number is not normal! ");

http://www.bkjia.com/PHPjc/633116.html www.bkjia.com true http://www.bkjia.com/PHPjc/633116.html techarticle A character in a string specified in PHP is replaced with an asterisk we have a function that can be implemented such as Substr,preg_replace,substr_replace and so on, I give these three functions respectively ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.