How to convert string to lowercase with PHP

PHPz
Release: 2023-03-22 16:04:01
Original
1569 people have browsed it

In web development, PHP is one of the most numerous server-side scripting languages. For PHP developers, sometimes it is necessary to convert string case to uppercase and lowercase. In this article, we will take a deep dive into how to convert a string to lowercase in PHP.

In PHP, there are multiple ways to convert a string to lowercase. Here are some of the methods:

  1. strtolower() function

The strtolower() function can convert a string to lowercase letters. The syntax of this function is as follows:

string strtolower (string $string)
Copy after login

Among them, the $string parameter is the string to be converted.

The following is sample code to convert a string to lowercase using the strtolower() function:

$str = "PHP 转小写";
echo strtolower($str);
Copy after login

Output:

php 转小写
Copy after login
Copy after login
Copy after login
  1. mb_strtolower() function

mb_strtolower() function is a function in the mbstring function extension, which can convert strings to lowercase letters and supports multi-byte character sets. The syntax of this function is as follows:

string mb_strtolower (string $str [, string $encoding = mb_internal_encoding()])
Copy after login

Among them, the $str parameter is the string to be converted, the $encoding parameter is the character set encoding, and the default value is mb_internal_encoding().

The following is sample code to convert a string to lowercase using the mb_strtolower() function:

$str = "PHP 转小写";
echo mb_strtolower($str);
Copy after login

Output:

php 转小写
Copy after login
Copy after login
Copy after login
  1. strtolower() and mb_strtolower The difference between ()

Although both methods can convert strings to lowercase letters, the strtolower() function only supports single-byte character sets, while the mb_strtolower() function not only supports Single-byte character set, also supports multi-byte character sets, such as UTF-8, etc.

  1. Custom function

In addition to the built-in functions, you can also customize functions to convert PHP strings to lowercase. The following is a sample code for a custom function:

function toLower($str) {
    $res = "";
    for ($i = 0; $i < strlen($str); $i++) {
        $char = ord($str[$i]); // 将字符转换为ASCII码
        if ($char >= 65 && $char <= 90) { // 判断字符是否为大写字母
            $res .= chr($char + 32); // ASCII码加32,得到对应的小写字母
        } else {
            $res .= $str[$i]; // 如果不是大写字母,直接拼接上去
        }
    }
    return $res;
}

$str = "PHP 转小写";
echo toLower($str);
Copy after login

Output:

php 转小写
Copy after login
Copy after login
Copy after login

Summary

This article introduces several ways to convert PHP to lowercase letters Methods, including using the built-in functions strtolower() and mb_strtolower(), as well as custom functions. To ensure code portability and multi-language support, it is recommended to use the mb_strtolower() function.

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