PHP mb_convert_case Use

Source: Internet
Author: User

Mb_convert_case
  • (PHP 4 >= 4.3.0, PHP 5, PHP 7)
  • Mb_convert_case-perform case Folding on a string
  • Mb_convert_case-converting a string to uppercase and lowercase
Description
mb_convert_case($str,$mode[,$encodingmb_internal_encoding()])//Performs case folding on a string, converted in the way specified by mode.//对一个 string 进行大小写转换,转换模式由 mode 指定。
Parametersstr
    • The string being converted.
    • The string to be converted.
Mode
    • The mode of the conversion. It can be one of mb_case_upper, Mb_case_lower, or Mb_case_title.
    • The mode of the transformation. It can be MB_CASE_UPPER one MB_CASE_LOWER of, and MB_CASE_TITLE .
Encoding
    • The encoding parameter is the character encoding. If It is omitted, the internal character encoding value would be used.
    • The encoding parameter is a character encoding. If omitted, the internal character encoding is used.
Return Values
    • A case folded version of string converted in the specified by mode.
    • Converts a string-case version to the mode specified by mode.
Examples
<?php/*** Created by Phpstorm.* User:zhangrongxiang* DATE:2018/1/28* Time: PM 3:16 *//**example #1 mb_convert_case () example */$str="Mary Had a Little lamb and she loved it so";$str=Mb_convert_case( $str, Mb_case_upper, "UTF-8" ).Php_eol;Echo $str; //Output MARY had A LITTLE LAMB and SHE loved IT so$str=Mb_convert_case( $str, Mb_case_title, "UTF-8" ).Php_eol;Echo $str; //Output Mary had A Little Lamb and She loved It so/**example #2 mb_convert_case () example of non-Latin UTF-8 text */$str="Τ?χιστηαλ?πηξβαφ??" Ψημ?νηγη,δρασκελ?ζειυπ?ρνωθρο? κυν?? ";$str=Mb_convert_case( $str, Mb_case_upper, "UTF-8" ).Php_eol;Echo $str; //Output τ?χιστηαλ?πηξβαφ?σψημ?νηγη,δρασκελ?ζειυπ?ρνωθρ0?κυν?σ$str=Mb_convert_case( $str, Mb_case_title, "UTF-8" ).Php_eol;Echo $str; //Output τ?χιστηαλ?πηξβαφ?σψημ?νηγη,δρασκελ?ζειυπ?ρνωθρο?κυν?σ/*** Mb_strtolower ()-Make string lowercase* Mb_strtoupper ()-Capitalize the string* Strtolower ()-Converts a string to lowercase* Strtoupper ()-Converts a string to uppercase* Ucfirst ()-Converts the first letter of a string to uppercase* Ucwords ()-Converts the first letter of each word in a string to uppercase */Echo Mb_convert_case( ' aaa ' aaa "', Mb_case_title ).Php_eol; //aaa "Aaa"//But I want this ===> aaa "AAA"functionMb_convert_case_utf8_variation( $s ){$arr=Preg_split( "//u", $s,-1, Preg_split_no_empty );    Var_dump($arr);    $result="";    $mode=false;    foreach ( $arr  as $char ){$res=Preg_match(                   '/\\p{mn}|\\p{me}|\\p{cf}|\\p{lm}|\\p{sk}|\\p{lu}|\\p{ll}| '.'\\p{lt}|\\p{sk}|\\p{cs}/u ', $char )==1;        if ( $mode ){if (!$res ){$mode=false;}        }ElseIf ( $res ){$mode=true;            $char=Mb_convert_case( $char, Mb_case_title, "UTF-8" );}$result.=$char;}return $result;}EchoMb_convert_case_utf8_variation(' aaa ' aaa "').Php_eol;//aaa "AAA"Echo Mb_convert_case("Hello China",Mb_case_upper).Php_eol;//hello ChinaEcho Mb_convert_case("Hello China",Mb_case_upper,"GBK").Php_eol;//hello China
Extensionunicode
    • By contrast-to-the-folding functions such as strtolower () and Strtoupper (), case folding are performed on the Basis of the Unicode character properties. Thus The behaviour of this function isn't affected by locale settings and it can convert any characters that has ' Alphab Etic ' property, such as A-umlaut (?).
    • The execution of case conversions is based on the Unicode character attributes, compared to the standard case conversion functions like Strtolower (), Strtoupper (). Therefore, the behavior of this function is not affected by locale settings and is capable of converting any character with a "letter" attribute, such as a vowel tone a (?).
    • For more information about the Unicode properties, please see? http://www.unicode.org/unicode/reports/tr21/.
    • For more information on Unicode properties, see? http://www.unicode.org/unicode/reports/tr21/.
UTF-8 Coding Rules
    • For a single-byte symbol, the first bit of the byte is set to 0, and the next 7 bits are the Unicode code for the symbol.
      So for the English alphabet, the UTF-8 encoding and ASCII code are the same.

    • For n-byte symbols (n > 1), the first n bits are set to 1, the Nth + 1 bits are set to 0,
      The first two bits of the latter byte are set to 10. The rest of the bits are not mentioned, all of which are Unicode codes for this symbol.

Unicode符号范围     |        UTF-8编码方式(十六进制)          |              (二进制)----------------------+---------------------------------------------0000 0000-0000 007F | 0xxxxxxx0000 0080-0000 07FF | 110xxxxx 10xxxxxx0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

According to the above table, it is very simple to interpret UTF-8 coding. If the first bit of a byte is 0, then this byte
is a single character, and if the first bit is 1, how many consecutive 1 is the number of bytes that the current character occupies.
Because the previous bit of the multibyte Utf-8 encoded value begins with 1.

Article reference
    • http://php.net/manual/en/function.mb-convert-case.php
Reprint Annotated Source

PHP mb_convert_case Use

Related Article

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.