Home  >  Article  >  Backend Development  >  How to convert all strings to lowercase in php

How to convert all strings to lowercase in php

青灯夜游
青灯夜游Original
2022-09-27 19:00:464469browse

3 conversion methods: 1. Use strtolower() function for conversion, syntax "strtolower(string)"; 2. Use mb_strtolower() function for conversion, syntax "mb_strtolower(string, character encoding) "; 3. Use the mb_convert_case() function to convert, the syntax is "mb_convert_case(string, MB_CASE_LOWER, character encoding)".

How to convert all strings to lowercase in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

In PHP, you want to convert a string into To convert all the letters to lowercase, you can use the strtolower(), mb_strtolower() or mb_convert_case() function

Method 1: Use the strtolower() function to convert

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
header("Content-type:text/html;charset=utf-8");
$str = "HTTPS://WWW.PHP.CN/";
echo "原字符串:<br> $str <br><br>";
$str = strtolower($str);
echo "转换后:<br> $str";
?>

The running results are as follows:

How to convert all strings to lowercase in php

Method 2 : Use the mb_strtolower() function for conversion

The function of the mb_strtolower() function is similar to the strtolower() function. It can also convert letters in the string to lowercase, and the mb_strtolower() function You 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 (Ä).

Example 1:

<?php
header("Content-type:text/html;charset=utf-8");
$str = "HELLO";
echo "原字符串:<br> $str <br><br>";
$str = mb_strtolower($str, &#39;UTF-8&#39;);
echo "转换后:<br> $str";
?>

How to convert all strings to lowercase in php

Example 2:

<?php
header("Content-type:text/html;charset=utf-8");
$str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός";
echo "原字符串:<br> $str <br><br>";
$str = mb_strtolower($str, &#39;UTF-8&#39;);
echo "转换后:<br> $str";
?>

How to convert all strings to lowercase in php

Method 3: Use the mb_convert_case() function to convert

The mb_convert_case() function can convert strings from upper to lower case. The syntax format is as follows :

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 MB_CASE_UPPER (all converted to uppercase), MB_CASE_LOWER (all One of MB_CASE_TITLE (convert the first letter to uppercase); $encoding is the character encoding of the parameter and 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
header("Content-type:text/html;charset=utf-8");
$str = "ABCDEFG";
echo "原字符串:<br> $str <br><br>";
$str = mb_convert_case($str,MB_CASE_LOWER, &#39;UTF-8&#39;);
echo "转换后:<br> $str";
?>

How to convert all strings to lowercase in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert all strings to lowercase 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