Home >Backend Development >PHP Problem >How to achieve case conversion in php
Case conversion method: 1. Use strtolower() to convert characters to lowercase; 2. Use strtoupper() to convert characters to uppercase; 3. Use ucfirst() to convert strings Convert the first letter of each word to uppercase; 4. Use ucwords() to convert the first letter of each word in the string to uppercase.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php realizes upper and lower case Conversion function
#1, strtolower() function--convert characters into lowercase
strtolower() function The letters in the string can be converted 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.
<?php $str = "HTTPS://WWW.PHP.CN/"; $str = strtolower($str); echo $str; ?>
2. Use the strtoupper function--convert characters to uppercase
strtoupper() function The letters in the string can be converted 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.
<?php $str = "https://www.php.cn/"; $str = strtoupper($str); echo $str; ?>
3. Use the ucfirst() function--convert the first letter to uppercase
The ucfirst function can convert the first letter of the string into uppercase The first letter is converted to uppercase. The syntax format is as follows:
ucfirst($string)
Among them, $string is the string that needs to be converted.
<?php $str = 'hello world!'; $str = ucfirst($str); echo $str.'<br>'; $str2 = 'HELLO WORLD!'; $str2 = ucfirst(strtolower($str2)); echo $str2; ?>
4. Use the ucwords() function--convert the first letter of each word to uppercase
ucwords() The function can convert the first letter of each word in the string to uppercase. The syntax format is as follows:
ucwords($string [, $delimiters = "\t\r\n\f\v" ])
Among them, $string is the string that needs to be converted; $delimiters is an optional parameter, used to indicate word separation. Characters, the default are space characters, tab characters, line feed characters, carriage return characters, horizontal lines and vertical lines.
<?php $str = 'hello world!'; $str = ucwords($str); echo $str.'<br>'; $str2 = 'HELLO WORLD!'; $str2 = ucwords(strtolower($str2)); echo $str2; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to achieve case conversion in php. For more information, please follow other related articles on the PHP Chinese website!