Home > Backend Development > PHP Problem > How to hide some content in php

How to hide some content in php

藏色散人
Release: 2023-03-15 06:44:01
Original
3017 people have browsed it

How to hide part of the content in php: 1. Create a PHP sample file; 2. Use "function hidestr($string, $start = 0, $length = 0, $re = '*') {. ..}" method can be used to hide it.

How to hide some content in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

php How to hide part of the content?

PHP functions to hide some strings (such as name, username, ID card, IP, mobile phone number, etc.)

The usage of this function is almost the same as the PHP built-in function substr(). it's the same. It’s just that substr() is used for interception, while hidestr() is used for hiding~

/**
 * 将一个字符串部分字符用$re替代隐藏
 * @param string    $string   待处理的字符串
 * @param int       $start    规定在字符串的何处开始,
 *                            正数 - 在字符串的指定位置开始
 *                            负数 - 在从字符串结尾的指定位置开始
 *                            0 - 在字符串中的第一个字符处开始
 * @param int       $length   可选。规定要隐藏的字符串长度。默认是直到字符串的结尾。
 *                            正数 - 从 start 参数所在的位置隐藏
 *                            负数 - 从字符串末端隐藏
 * @param string    $re       替代符
 * @return string   处理后的字符串
 */
function hidestr($string, $start = 0, $length = 0, $re = '*') {
    if (empty($string)) return false;
    $strarr = array();
    $mb_strlen = mb_strlen($string);
    while ($mb_strlen) {//循环把字符串变为数组
        $strarr[] = mb_substr($string, 0, 1, 'utf8');
        $string = mb_substr($string, 1, $mb_strlen, 'utf8');
        $mb_strlen = mb_strlen($string);
    }
    $strlen = count($strarr);
    $begin  = $start >= 0 ? $start : ($strlen - abs($start));
    $end    = $last   = $strlen - 1;
    if ($length > 0) {
        $end  = $begin + $length - 1;
    } elseif ($length < 0) {
        $end -= abs($length);
    }
    for ($i=$begin; $i<=$end; $i++) {
        $strarr[$i] = $re;
    }
    if ($begin >= $end || $begin >= $last || $end > $last) return false;
    return implode(&#39;&#39;, $strarr);
}
//隐藏手机号中间4位
hidestr(&#39;18600005940&#39;, 3, 4); //186****5940
//只保留姓名里的最后一个字,常见与ATM,网银等
hidestr(&#39;谢世亮&#39;, 0, -1); //**亮
//隐藏邮箱部分内容,常见网站帐号,如支付宝等
list($name, $domain) = explode(&#39;@&#39;, &#39;979137@qq.com&#39;);
hidestr($name, 1, -1) . &#39;@&#39; . hidestr($domain, 0, 2); // 9****7@**.com
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to hide some content in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template