Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use php to detect whether a string is utf-8 encoded

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use php to detect whether the string is utf-8 coding". In the daily operation, I believe many people have doubts about how to use php to detect whether the string is utf-8 coding. The editor consulted all kinds of data and sorted out a simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "how to use php to detect whether the string is utf-8 coding". Next, please follow the editor to study!

In php development, transcoding functions are sometimes used, such as iconv (), mb_convert_encoding () functions. When transcoding or decoding with functions, we sometimes need to determine the current string encoding type, which is better than utf-8 encoding, and then transcoding. The following is the judgment function of good php about UTF-8 coding that is widely used on the Internet in web development. The code is as follows:

Function is_utf8 ($string) / / function 1 {/ / From http://w3.org/International/questions/qa-forms-utf-8.htmlreturn preg_match ('% ^ (?: [\ x09\ x0A\ x0D\ x20 -\ x7E] # ASCII | [\ xC2-\ xDF] [\ x80 -\ xBF] # non-overlong 2-byte |\ xE0 [\ xA0-\ xBF] [\ x80 -\ xBF] # excluding overlongs | [\ xE1-\ xEC\ xEE\ xEF] [\ x80 -\ xBF] {2} # straight 3-byte |\ xED [\ x80 -\ x9F] [\ x80 -\ xBF] # excluding surrogates |\ xF0 [\ x90 -\ xBF] [\ x80 -\ xBF] {2} # planes 1-3 | [\ xF1-\ xF3] [\ x80 -\ xBF] {3} # planes 4-15 |\ xF4 [\ x80 -\ x8F] [\ x80 -\ xBF] {2} # plane 16) * $% xs' $string) } function mb_is_utf8 ($string) / / function two {return mb_detect_encoding ($string, 'UTF-8') = =' UTF-8';}

The mb_detect_encoding () function is a built-in function of php that is used to determine the current string encoding type. This function has three parameters, the first parameter is the string to be judged, the second parameter is the comparative character encoding list, it can be a string or an array, and the third parameter is the requirement.

Hopefully, these two functions will help you with the Phper you need.

At this point, the study on "how to use php to detect whether a string is utf-8 encoding" 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report