In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to solve the problem of Chinese garbled code in php mbsubstr". In daily operation, I believe many people have doubts about how to solve the problem of Chinese garbled code in php mbsubstr. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to solve the problem of Chinese garbled code in php mbsubstr". Next, please follow the editor to study!
Operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computer
How to solve the problem of garbled codes in php mbsubstr?
PHP uses substr to intercept strings. Mb_substr is used to solve the problem of Chinese garbled.
Example: mb_substr ('Test of intercepting Chinese garbled code', 0pc5, 'utf-8')
Syntax: string substr (string string, int start [, int length]) $rest = substr ("abcdef", 1); / / returns "bcdef" $rest = substr ("abcdef", 1,3); / / returns "bcd"
If start is negative, the returned string will start with the start at the end of the string.
$rest = substr ("abcdef",-1); / / returns "f" $rest = substr ("abcdef",-2); / / returns "ef" $rest = substr ("abcdef",-3,1); / / returns "d"
If the argument length is given and is positive, the returned string will return length characters from start.
If the argument length is given and it is negative, the returned string ends with the length character at the end of the string.
$rest = substr ("abcdef", 1,-1); / / returns "bcde"
There is no problem with English. Let's test a Chinese.
$rest = substr ("Chinese", 1,-1); / / returns "fdsafsda" is garbled
The result of this interception of characters is definitely not the result we want, and this situation of garbled codes in PHP substr may cause the program to fail to run properly.
There are two main solutions:
First, using the mb_substr () interception of the mbstring extension library will not cause garbled.
You can use the function mb_substr () / mb_strcut (). The use of mb_substr () / mb_strcut () is similar to substr (), except that one more parameter is added at the end of mb_substr () / mb_strcut to set the encoding of the string.
But most servers do not open php_mbstring.dll, so you need to open php_mbstring.dll in php.ini.
Echo mb_substr ("php Chinese character encode", 0jue 4, "utf-8")
If the last encoding parameter is not specified, it will be three bytes into one Chinese, which is the characteristic of utf-8 coding. If you add the utf-8 character set description, it is intercepted in a word unit.
When using, you should pay attention to the encoding of the php file and the coding of the web page display. To use this mb_substr method, you need to know the encoding of the string in advance, and if you don't know the encoding, you need to judge. The mbstring library also provides mb_check_encoding to verify the string encoding, but it is not perfect.
PHP comes with several string intercepting functions, among which substr and mb_substr are commonly used. When the former deals with Chinese, GBK is 2 length units and UTF is 3 length units. After the latter is specified, one Chinese language is 1 length unit.
Substr sometimes truncates 3 or half of Chinese characters and displays garbled codes. Relatively speaking, mb_substr is more suitable for us to use. But sometimes mb_substr doesn't seem to work so well. For example, I want to display a brief message of a small picture. Five Chinese words are just right. If more than five, intercept the first four and add "…" It is no problem to deal with Chinese in this way, but it is too short to intercept English or numbers.
Second, write the interception function by yourself, but it is not as efficient as using the mbstring extension library. Here is the function in ecshop to intercept the string encoded by UTF-8.
Function sub_str ($str, $length =, $append = true) {$str = trim ($str); $strlength = strlen ($str); if ($length = = | $length > = $strlength) {return $str; / / intercept the length equal to or greater than the length of this string and return the string itself} elseif ($length <) / / if the intercept length is negative {$length = $strlength + $length / / then the intercepting length is equal to the string length minus the intercepting length if ($length <) {$length = $strlength;// if the absolute value of the intercepting length is greater than the string itself length, then the intercepting length takes the string itself length}} if (function_exists ('mb_substr')) {$newstr = mb_substr ($str, $length, EC_CHARSET) } elseif (function_exists ('iconv_substr')) {$newstr = iconv_substr ($str, $length, EC_CHARSET);} else {/ / $newstr = trim_right (substr ($str, $length)); $newstr = substr ($str, $length);} if ($append & $str! = $newstr) {$newstr. ='...';} return $newstr At this point, the study on "how to solve the problem of Chinese garbled code in php mbsubstr" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.