Home>Article>Backend Development> What are the functions for converting upper and lower case in php?

What are the functions for converting upper and lower case in php?

青灯夜游
青灯夜游 Original
2021-09-26 15:53:05 4289browse

php function to convert uppercase to lowercase: 1. strtoupper(), which can convert the string to uppercase; 2. strtolower(), which can convert the string to lowercase; 3. ucfirst(); 4. lcfirst (); 5. ucwords(); 6. mb_strtoupper(), etc.

What are the functions for converting upper and lower case in php?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In Web development, there is a lot of data that needs to be Regularity makes it easier for administrators to manage. Therefore, when storing some data, it is necessary to uniformly handle the upper and lower case of letters. However, in order to facilitate user input, users are not deliberately required to enter uppercase or lowercase letters. Instead, when storing data, program control is used to store the input content in uppercase or lowercase letters.

PHP provides us with a lot of predefined functions, including functions for string case conversion, as shown in the following table:

Let’s introduce them separately.

1) strtoupper

strtoupper() function can convert the letters in the string to uppercase. The syntax format is as follows:

strtoupper($string)

Among them, $ string is a parameter of string type. This function can convert the letters in the parameter $string to uppercase and return the converted string.

The sample code is as follows:

The running results are as follows:

HTTPS://WWW.PHP.CN/

2) mb_strtoupper

mb_strtoupper() function has the same function The strtoupper() function is similar. It can also convert letters in the string to uppercase, and the mb_strtoupper() function can also set the character encoding of the parameters. Its syntax format is as follows:

mb_strtoupper($str [, $encoding = mb_internal_encoding()])

Among them, $str needs to be converted string, $encoding is an optional parameter used to set the character encoding of the parameter.

The difference from the strtoupper() function is that the letters in $str are determined through the Unicode character attribute. Therefore, the mb_strtoupper() function is not affected by the locale setting and can convert any character with a "letter" attribute, such as a umlaut (ä).

The sample code is as follows:

'; $str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός"; $str = mb_strtoupper($str, 'UTF-8'); echo $str; ?>

The running results are as follows:

What are the functions for converting upper and lower case in php?

##3) strtolower## The #strtolower() function can convert the letters in the string to lowercase. The syntax format is as follows:

strtolower($string)

Among them, $string is a parameter of string type. This function can convert the letters in the parameter $string. to lowercase, and return the converted string.

The sample code is as follows:

The running results are as follows:

HTTPS://WWW.PHP.CN/

The function of mb_strtolower() function is the same as The strtolower() function is similar and can also convert letters in the string to lowercase, and the mb_strtolower() function can also set the character encoding of the parameter. The syntax format is as follows:

mb_strtolower($str [, $encoding = mb_internal_encoding()])

Among them, $str is the string that needs to be converted, and $encoding is an optional parameter used to set the character encoding of the parameter.

The difference from the strtolower() function is that the detection of alphabetic characters in $str is based on the Unicode attribute of the character. The behavior of the function is therefore independent of the language setting and can convert any character with a "letter" attribute, such as the umlaut A (Ä).

The sample code is as follows:

'; $str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός"; $str = mb_strtolower($str, 'UTF-8'); echo $str; ?>

The running result is as follows:

What are the functions for converting upper and lower case in php?##5) ucfirst

## The #ucfirst function converts the first letter of a string to uppercase. The syntax format is as follows:

The sample code is as follows:

'; $str2 = 'HELLO WORLD!'; $str2 = ucfirst(strtolower($str2)); echo $str2; ?>

The running results are as follows:

Hello world! Hello world!

lcfirst() function can make a The first character of the string is converted to lowercase, and the syntax format is as follows:

The sample code is as follows:

'; $str2 = 'HELLO WORLD!'; $str2 = lcfirst($str2); echo $str2; ?>

The running results are as follows:

Hello world! Hello world!

ucwords() function can convert characters The first letter of each word in the string is converted to uppercase. The syntax format is as follows:

The sample code is as follows:

'; $str2 = 'HELLO WORLD!'; $str2 = ucwords(strtolower($str2)); echo $str2; ?>

The running result is as follows:

Hello world! Hello world!

mb_convert_case() function can convert characters String is converted to uppercase and lowercase, the syntax format is as follows:

Compared with the strtolower() and strtoupper() functions, the mb_convert_case() function performs case conversion based on Unicode character attributes. Therefore, the behavior of the mb_convert_case() function is not affected by the locale setting and can convert any character with a "letter" attribute, such as the umlaut A (Ä).

The sample code is as follows:

'; $str = mb_convert_case($str, MB_CASE_LOWER, "UTF-8"); echo $str.'
'; $str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8"); echo $str.'
'; ?>

The running results are as follows:

##Recommended learning: "

"What are the functions for converting upper and lower case in php?

##lcfirst Convert the first letter of the string to lowercase ucwords Convert the first character of each word in the string to uppercase mb_strtoupper Convert the string to uppercase (different from the strtoupper function) mb_strtolower Convert the character Convert the string to lowercase (different from the strtolower function) mb_convert_case Convert the string according to different modes 4) mb_strtolower
ucfirst($str)
Among them, $str is the string that needs to be converted.6) lcfirst
lcfirst($str)
Among them, $str is the string that needs to be converted.7) ucwords
ucwords($str)
Among them, $str is the string that needs to be converted; $delimiters is an optional parameter used to represent word delimiters. The default is Space, tab, line feed, carriage return, horizontal and vertical lines.8) mb_convert_case
mb_convert_case($str, $mode [, $encoding = mb_internal_encoding()])
Among them, $str is the string that needs to be converted; $mode is the conversion mode, which can be one of MB_CASE_UPPER, MB_CASE_LOWER and MB_CASE_TITLE; $encoding is the parameter Character encoding, can be omitted.PHP Video Tutorial
Function name Function
strtoupper Convert string to uppercase
strtolower Convert the string to lowercase
ucfirst Convert the first letter of the string to uppercase

The above is the detailed content of What are the functions for converting upper and lower case in php?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn