How to replace asterisks in Chinese by php
This article mainly explains "how php replaces asterisks in Chinese". The explanation in the article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "how php replaces asterisks in Chinese".
Php will replace the asterisk in Chinese: 1, to determine whether the character contains Chinese characters; 2, to use the mb_substr function to replace the specified Chinese part with an asterisk.
This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer
How does php replace the asterisk in Chinese?
PHP user names are handled with asterisks
The PHP user name is processed with the * number:
User name: English, Chinese, Chinese and English mixed, Chinese and English characters mixed
The treatment is as follows: the first letter and the end are retained, and the * sign is replaced in the middle (one character is directly displayed, two characters: Zhang *, three or more characters: song * Dan)
First of all, determine whether the character contains Chinese characters, if it is processed by using mb_ series functions (the Chinese here is in UTF-8 format)
UTF-8 Chinese regular: "/ [\ x {4e00} -\ x {9fa5}] + / u"
GB2312 Chinese regular: "/ [" .chr (0xa1). "-" .chr (0xff). "] + /"
/ / determine whether to include the Chinese characters if ("/ [\ x {4e00} -\ x {9fa5}] + / u", $str) {/ / calculate the length $len = mb_strlen ($str, 'UTF-8') according to the Chinese characters; / / echo' Chinese' If ($len > = 3) {/ / three or more characters, with * instead of $str = mb_substr ($str, 0,1, 'UTF-8'). '*'. Mb_substr ($str,-1,1, 'UTF-8');} elseif ($len = = 2) {/ / two characters $str = mb_substr ($str, 0,1,' UTF-8'). Else {/ / calculates the length of $len = strlen ($str) according to the English string; / / echo 'English'; if ($len > = 3) {/ / three or more characters, with * instead of $str = substr ($str, 0,1). '*'. Substr ($str,-1);} elseif ($len = = 2) {/ / two characters $str = substr ($str2, 0,1). '*;}}
Thank you for reading, the above is the content of "how php replaces asterisks in Chinese". After the study of this article, I believe you have a deeper understanding of how php replaces asterisks in Chinese, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!