Home >Backend Development >PHP Problem >How to hide some content in php
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.
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('', $strarr); } //隐藏手机号中间4位 hidestr('18600005940', 3, 4); //186****5940 //只保留姓名里的最后一个字,常见与ATM,网银等 hidestr('谢世亮', 0, -1); //**亮 //隐藏邮箱部分内容,常见网站帐号,如支付宝等 list($name, $domain) = explode('@', '979137@qq.com'); hidestr($name, 1, -1) . '@' . hidestr($domain, 0, 2); // 9****7@**.com
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!