Home > Backend Development > PHP Tutorial > How to convert string to uppercase and lowercase in php?

How to convert string to uppercase and lowercase in php?

怪我咯
Release: 2023-03-07 17:56:01
Original
5190 people have browsed it

In website development, there is a lot of data that needs to be regular and easy for administrators to manage. Therefore, when submitting requests for some stored data, case requirements are required. However, in order to facilitate user input, we do not deliberately require users to enter uppercase or lowercase letters. Instead, when storing data, program control is used to store the input content in uppercase or lowercase letters. In this chapter, we will show you how to convert the string we input to uppercase or lowercase.

First we learn two functions: strtoupper() function and strtolower() function

Method to convert all strings into uppercase:

strtoupper() function

strtoupper() function converts the string to uppercase. The syntax is as follows:

strtoupper (str )
Copy after login

The parameter str is converted String

Example

Use the strtoupper() function to convert the following variable $str to uppercase. The code is as follows

<?php
$str = "Www.Php.cn";
$str = strtoupper($str);
echo $str;
?>
Copy after login

Code running results:

How to convert string to uppercase and lowercase in php?

Convert all strings to lowercase:

strtolower() function

strtolower() function is to All strings are converted to lowercase, the syntax is as follows:

strtolower (str )
Copy after login

The parameter str is the converted string

Example

Use the strtolower() function to convert the variable $str to lowercase , the code is as follows:

<?php
$str = "WWW.PHP.CN";
$str = strtolower($str);
echo $str;
?>
Copy after login

Code running results:

How to convert string to uppercase and lowercase in php?

Application example

The following example fills in the user The book numbers in the book information are all stored in uppercase. The code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>字符串大小写转换</title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
    <p>
        图书名称:<input type="text" name="name">
    </p>

    <p>
        编&#12288&#12288号:<input type="text" name="cod">
    </p>
    <p>
        价&#12288&#12288格:<input type="text" name="pri">
    </p>

    <p>
        数&#12288&#12288量:<input type="text" name="num">
    </p>
    <p>
        产&#12288&#12288地:<input type="text" name="pro">
    </p>

    <input type="submit" name="sub" value="提交" />
</form>
</body>
</html>
<?PHP
header("content-type:text/html;charset=utf-8");
$name=$_POST[&#39;name&#39;];
$cod=$_POST[&#39;cod&#39;];
$pri=$_POST[&#39;pri&#39;];
$num=$_POST[&#39;num&#39;];
$pro=$_POST[&#39;pro&#39;];
if(isset($_POST[&#39;sub&#39;])){
 $name=strtoupper($name);
 $cod=strtoupper($cod);
 echo "<p>图书名称:$name<p/>";
 echo "<p>编号:$cod<p/>";
 echo "<p>价格:$pri<p/>";
 echo "<p>数量:$num<p/>";
 echo "<p>产地:$pro<p/>";
}
?>
Copy after login

The code running result:

How to convert string to uppercase and lowercase in php?

Conduct the string Case conversion is not a necessary function and can be selected according to the different requirements of each website. For example, in the backend product addition page of an e-commerce website, in order to display product models with unified specifications, you can use the two case conversion functions explained in this chapter. So it is very necessary to master these two functions. For more questions about strings, please pay attention to How to convert string to uppercase and lowercase in php?php string stringspecial topic

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