Home > Backend Development > PHP Problem > How to convert uppercase letters to lowercase letters in php

How to convert uppercase letters to lowercase letters in php

青灯夜游
Release: 2023-03-10 19:34:02
Original
5166 people have browsed it

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.

How to convert uppercase letters to lowercase letters in php

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)
Copy after login

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;
?>
Copy after login

The running result is as follows:

//m.sbmmt.com/
Copy after login

2) lcfirst

lcfirst() function The first character of a string can be converted to lowercase. The syntax format is as follows:

lcfirst($str)
Copy after login

where $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 result is as follows:

hello World!
hELLO WORLD!
Copy after login

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

The running results are as follows:

//m.sbmmt.com/
τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός
Copy after login

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!

Related labels:
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