Home > Article > Backend Development > How to convert uppercase letters to lowercase letters in php
Conversion method: 1. Use the "strtolower (string)" statement to convert the string to lowercase. 2. Use the "lcfirst(string)" statement to convert the first letter of the string to lowercase; 3. Use the "mb_strtolower(string, character encoding)" statement to convert the string to lowercase.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php converts uppercase letters to lowercase Letters
Function name | Function |
---|---|
strtolower | Convert the string Convert to lowercase |
lcfirst | Convert the first letter of the string to lowercase |
mb_strtolower | Convert the string to lowercase (difference from strtolower function) |
1) strtolower
strtolower() function can convert letters in a 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 parameter $string Convert the letters in to lowercase and return the converted string.
The sample code is as follows:
<?php $str = "HTTPS://WWW.PHP.CN/"; $str = strtolower($str); echo $str; ?>
The running result is as follows:
https://www.php.cn/
2) lcfirst
lcfirst() function The first character of a string can be converted to lowercase. The syntax format is as follows:
lcfirst($str)
where $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; ?>
The running result is as follows:
hello World! hELLO WORLD!
3) mb_strtolower
mb_strtolower The function of the () function is similar to the strtolower() function. It 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:
<?php $str = "HTTP://WWW.PHP.CN/"; $str = mb_strtolower($str); echo $str.'<br>'; $str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός"; $str = mb_strtolower($str, 'UTF-8'); echo $str; ?>
The running results are as follows:
https://www.php.cn/ τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert uppercase letters to lowercase letters in php. For more information, please follow other related articles on the PHP Chinese website!