A method to implement UTF8 encoding of Chinese character conversion with PHP custom function _php Skills

Source: Internet
Author: User
Tags ord php programming php regular expression

This paper illustrates the method of PHP custom function to realize Chinese character conversion UTF8 encoding. Share to everyone for your reference, specific as follows:

The function is composed of two parts:

function Htou ($c) {
  $n = (ord ($c [0]) & 0x1f) <<;
  $n + = (ord ($c [1]) & 0x3f) << 6;
  $n + + ord ($c [2]) & 0x3f;
  return $n;
}
A string
function Utf8_unicode ($str) {
  $encode = ' that hides the UTF8 format in code;
  For ($i =0 $i <strlen ($STR); $i + +) {
    if Ord (substr ($str, $i, 1) > 0xa0) {
      $encode. = ' &# '. Htou (substr ($str, $i, 3)). '
      ; $i +=2;
    } else {
      $encode. = ' &# '. Ord ($str [$i]). '; ';
    }
  return $encode;
}

We can use Utf8_unicode () to convert Chinese characters and return to the UTF8 encoding of Chinese characters. This way the output page will not be garbled.

PS: Here again for you to recommend a few very practical online coding conversion tools for everyone to reference the use of:

UTF-8 Code conversion Tool:
Http://tools.jb51.net/transcoding/convutf

Online Coding Conversion Tool (UTF-8/UTF-32/PUNYCODE/BASE64):
Http://tools.jb51.net/transcoding/decode_encode_tool

Native/utf-8 Online Code conversion tool:
Http://tools.jb51.net/transcoding/native2utf8

For more information about PHP interested readers can view the site topics: "PHP coding and transcoding Operation tips Summary", "PHP object-oriented Programming Introduction Course", "PHP Mathematical Calculation Skills Summary", "PHP Array" Operation Techniques Encyclopedia, "PHP string (String) Usage summary, "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", "PHP Regular Expression Usage Summary", and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.