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

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

青灯夜游
Release: 2023-03-13 06:20:02
Original
4455 people have browsed it

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:

##lcfirst Convert the first letter of the string to lowercaseucwordsConvert the first character of each word in the string to uppercasemb_strtoupperConvert the string to uppercase (different from the strtoupper function) mb_strtolowerConvert the character Convert the string to lowercase (different from the strtolower function)mb_convert_caseConvert 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.&#39;<br>&#39;;
$str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός";
$str = mb_strtoupper($str, &#39;UTF-8&#39;);
echo $str; 
?>
Copy after login

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)
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.&#39;<br>&#39;;
$str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός";
$str = mb_strtolower($str, &#39;UTF-8&#39;);
echo $str;
?>
Copy after login

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:

ucfirst($str)
Copy after login
Among them, $str is the string that needs to be converted.

The sample code is as follows:

<?php
    $str = &#39;hello world!&#39;;
    $str = ucfirst($str);
    echo $str.&#39;<br>&#39;;
    $str2 = &#39;HELLO WORLD!&#39;;
    $str2 = ucfirst(strtolower($str2));
    echo $str2;
?>
Copy after login

The running results are as follows:

Hello world!
Hello world!
Copy after login

6) lcfirst

lcfirst() 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 = &#39;Hello World!&#39;;
    $str = lcfirst($str);
    echo $str.&#39;<br>&#39;;
    $str2 = &#39;HELLO WORLD!&#39;;
    $str2 = lcfirst($str2);
    echo $str2;
?>
Copy after login

The running results are as follows:

hello World!
hELLO WORLD!
Copy after login

7) ucwords

ucwords() 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 = &#39;hello world!&#39;;
    $str = ucwords($str);
    echo $str.&#39;<br>&#39;;
    $str2 = &#39;HELLO WORLD!&#39;;
    $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_case

mb_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.&#39;<br>&#39;;
$str = mb_convert_case($str, MB_CASE_LOWER, "UTF-8");
echo $str.&#39;<br>&#39;;
$str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
echo $str.&#39;<br>&#39;;
?>
Copy after login

The running results are as follows:

##Recommended learning: "

PHP Video Tutorial

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

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template