Home >Backend Development >PHP Problem >How to achieve case conversion in php

How to achieve case conversion in php

青灯夜游
青灯夜游Original
2022-02-09 17:42:287073browse

Case conversion method: 1. Use strtolower() to convert characters to lowercase; 2. Use strtoupper() to convert characters to uppercase; 3. Use ucfirst() to convert strings Convert the first letter of each word to uppercase; 4. Use ucwords() to convert the first letter of each word in the string to uppercase.

How to achieve case conversion in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php realizes upper and lower case Conversion function

#1, strtolower() function--convert characters into lowercase

strtolower() function The letters in the string can be converted to lowercase. The syntax format is as follows:

strtolower($string)

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.

<?php
$str = "HTTPS://WWW.PHP.CN/";
$str = strtolower($str);
echo $str;
?>

How to achieve case conversion in php

2. Use the strtoupper function--convert characters to uppercase

strtoupper() function The letters in the string can be converted to uppercase. The syntax format is as follows:

strtoupper($string)

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.

<?php
    $str = "https://www.php.cn/";
    $str = strtoupper($str);
    echo $str;
?>

How to achieve case conversion in php

3. Use the ucfirst() function--convert the first letter to uppercase

The ucfirst function can convert the first letter of the string into uppercase The first letter is converted to uppercase. The syntax format is as follows:

ucfirst($string)

Among them, $string is the string that needs to be converted.

<?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;
?>

How to achieve case conversion in php

4. Use the ucwords() function--convert the first letter of each word to uppercase

ucwords() The function can convert the first letter of each word in the string to uppercase. The syntax format is as follows:

ucwords($string [, $delimiters = "\t\r\n\f\v" ])

Among them, $string is the string that needs to be converted; $delimiters is an optional parameter, used to indicate word separation. Characters, the default are space characters, tab characters, line feed characters, carriage return characters, horizontal lines and vertical lines.

<?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;
?>

How to achieve case conversion in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to achieve case conversion 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