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:
Function name |
Function |
strtoupper |
Convert string to uppercase |
strtolower | Convert the string to lowercase |
ucfirst |
Convert the first letter of the string to uppercase |
##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 |
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)
Copy after login
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:
<?php
$str = "https://www.php.cn/";
$str = strtoupper($str);
echo $str;
?>
Copy after login
The running results are as follows:
HTTPS://WWW.PHP.CN/
Copy after login
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()])
Copy after login
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:
<?php
header("Content-type:text/html;charset=utf-8");
$str = "https://www.php.cn/";
$str = mb_strtoupper($str);
echo $str.'<br>';
$str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός";
$str = mb_strtoupper($str, 'UTF-8');
echo $str;
?>
Copy after login
The running results are as follows:
##3) strtolower## The #strtolower() function can convert the letters in the string to lowercase. The syntax format is as follows:
strtolower($string)
Copy after login
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:
<?php
header("Content-type:text/html;charset=utf-8");
$str = "HTTPS://WWW.PHP.CN/";
$str = strtolower($str);
echo $str;
?>
Copy after login
The running results are as follows:
https://www.php.cn/
Copy after login
4) mb_strtolower 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()])
Copy after login
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:
<?php
header("Content-type:text/html;charset=utf-8");
$str = "HTTPS://WWW.PHP.CN/";
$str = strtolower($str);
echo $str.'<br>';
$str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός";
$str = mb_strtolower($str, 'UTF-8');
echo $str;
?>
Copy after login
The running result is as follows:
##5) ucfirst
## The #ucfirst function converts the first letter of a string to uppercase. The syntax format is as follows: ucfirst($str)
Copy after login
Among them, $str is the string that needs to be converted.
The sample code is as follows:
<?php
$str = 'hello world!';
$str = ucfirst($str);
echo $str.'<br>';
$str2 = 'HELLO WORLD!';
$str2 = ucfirst(strtolower($str2));
echo $str2;
?>
Copy after login
The running results are as follows:
Hello world!
Hello world!
Copy after login
6) lcfirstlcfirst() function can make a The first character of the string is converted to lowercase, and the syntax format is as follows: lcfirst($str)
Copy after login
Among them, $str is the string that needs to be converted.
The sample code is as follows:
<?php
$str = 'Hello World!';
$str = lcfirst($str);
echo $str.'<br>';
$str2 = 'HELLO WORLD!';
$str2 = lcfirst($str2);
echo $str2;
?>
Copy after login
The running results are as follows:
hello World!
hELLO WORLD!
Copy after login
7) ucwordsucwords() function can convert characters The first letter of each word in the string is converted to uppercase. The syntax format is as follows: ucwords($str)
Copy after login
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.
The sample code is as follows:
<?php
$str = 'hello world!';
$str = ucwords($str);
echo $str.'<br>';
$str2 = 'HELLO WORLD!';
$str2 = ucwords(strtolower($str2));
echo $str2;
?>
Copy after login
The running result is as follows:
Hello World!
Hello World!
Copy after login
8) mb_convert_casemb_convert_case() function can convert characters String is converted to uppercase and lowercase, the syntax format is as follows: mb_convert_case($str, $mode [, $encoding = mb_internal_encoding()])
Copy after login
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.
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:
<?php
$str = "www.php.cn";
$str = mb_convert_case($str, MB_CASE_UPPER, "UTF-8");
echo $str.'<br>';
$str = mb_convert_case($str, MB_CASE_LOWER, "UTF-8");
echo $str.'<br>';
$str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
echo $str.'<br>';
?>
Copy after login
The running results are as follows:
##Recommended learning: "
PHP Video Tutorial"
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!