PHP String Case Conversion

Source: Internet
Author: User

Strtoupper (), Strtolower (), Ucfirst (), Ucfirst (), Ucwords (), Mb_strtoupper (), Mb_strtolower (), and Mb_convert_case () These eight functions differ and relate to:

Function name

Scope of Use

Function

Strtoupper

PHP4,PHP5

Convert a string to uppercase

Strtolower

PHP4,PHP5

Convert a string to lowercase

Ucfirst

PHP4,PHP5

Converts the first letter of a string to uppercase

Lcfirst

php5>= 5.3.0

Converts the first letter of a string to lowercase

Ucwords

PHP4,PHP5

Converts the first character of each word in a string to uppercase

Mb_strtoupper

php4>=4.3.0,php 5

Convert a string to uppercase (different from the strtoupper function)

Mb_strtolower

php4>=4.3.0,php 5

Convert a string to lowercase (different from the strtolower function)

Mb_convert_case

php4>=4.3.0,php 5

Convert strings according to different patterns

PHP provides a function for case-sensitive conversions
Array_change_key_case (Array,case)
Array required. Specifies the array to use.
Case Case_lower-default value, lowercase; case_upper uppercase.
Example:

$arr = Array (' ID ' = = 1, ' NAME ' = ' Yami ', ' Con ' = ' = ' = ' PHONE ' = ' = ' 123456 ', ' email ' = ' = ' [email protected] ' ));p Rint_r (Array_change_key_case ($arr, case_lower));


Unfortunately, this method can only implement conversion of one-dimensional arrays.
Here's a function to transform a multidimensional array.

/* * Convert array key value case * * @author    yamiliu<[email protected]> * @params    $aValue    array required to convert * @params 
    $case    boolean  0: lowercase, default, 1: Uppercase *  */function Changearrkey (& $aValue, $case = 0) {    foreach ($aValue as $key = = $item) {        if ($case) {            $keyTemp = Strtoupper ($key);        } else {            $keyTemp = Strtolower ($key); c13/>}         if ($keyTemp! = $key) {            unset ($aValue [$key]);            $aValue [$keyTemp] = $item;        }        if (Is_array ($item)) {            Changearrkey ($aValue [$keyTemp], $case);}}}    Call $arr = Array (' ID ' = = 1, ' NAME ' = ' = ' Yami ', ' Con ' = ' = ' = ' PHONE ' = ' = ' 123456 ', ' email ' = ' [email protec Ted]); Changearrkey ($arr); Var_dump ($arr);


Later think about changing the value of the original array is not a good note
Another way to write it is as follows:

/* * @author    yamiliu<[email protected]> * @params array   $aValue    required to convert * @params    Boolean $ Case      0: lowercase, default, 1: Uppercase * @return              array converted arrays  */function array_key_to_case ($aValue, $case = 0) {    $ avaluetemp = Array ();    foreach ($aValue as $key = + $item) {        if ($case) {            $keyTemp = Strtoupper ($key);        } else {            $keyTem p = strtolower ($key);        }        $aValueTemp [$keyTemp] = $item;        if (Is_array ($item)) {            $aValueTemp [$keyTemp] = Array_key_to_case ($item, $case);        }    }    return $aValueTemp;} Call $arr = array (' id ' = = 1, ' name ' = ' = ' Yami ', ' Con ' = ' = ' = ' phone ' = ' = ' 123456 ', ' email ' = ' [email protec Ted]); $r   = Array_key_to_case ($arr, 1);p Rint_r ($r);

PHP String Case Conversion

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.