


PHP string learning: Convert the size of the first character (3 methods)
In the previous article "PHP String Learning: Using Regular Expressions to Filter Characters and Return Numeric Characters", we introduced 3 methods of using regular expressions to extract numeric characters. This time we continue to learn and practice PHP strings and talk about how to convert the first letter of a string to uppercase or lowercase.
The main content of this article is "converting the case of the first character of a string", which can be to convert the first character of the entire string to uppercase or lowercase, or to convert each word in the string The case of the first character. Let’s introduce it to you below.
1. Convert the case of the first character in the string
<?php // 将字符串的第一个字符改为小写 echo lcfirst("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG")."<br>"; // 将字符串的第一个字符改为大写 echo ucfirst("the quick brown fox jumps over the lazy dog")."<br>"; ?>
Output result:
As can be seen, the lcfirst() and ucfirst() functions are used in the above code example to convert the case of the first character in the string. Let’s take a brief look at these two functions:
lcfirst() can convert the first character in the string to lowercase, and the syntax format is “
lcfirst($string)
". If you want to convert all characters in a string to lowercase (uniform string case), you can use thestrtolower($string)
function.ucfirst() can convert the first character in the string to uppercase, and the syntax format is "
ucfirst($string)
". If you want to convert all characters in a string to uppercase (unify string case), you can use thestrtoupper($string)
function.
But sometimes, we don’t need to convert the case of the first character in the entire string, but also need to convert the first character of each subsequent substring (word) separated by spaces. Convert to uppercase, how to do this? Let’s introduce it below:
2. Convert the first character of each word in the string to uppercase
<?php echo ucwords("the quick brown fox jumps over the lazy dog"); ?>
Output result:
As you can see, the ucwords() function is used in the above code example to convert the first character of each word to uppercase. Let’s take a brief look at this function:
ucwords() can convert the first character of each word in the string to uppercase, the syntax format is “
ucwords($string)
".
To summarize: How to convert string case in PHP
##strtolower($string)
The function converts all characters in the string
$stringto lowercase.
strtoupper($string)
The function converts all characters in the string
$stringto uppercase.
lcfirst($string)
The function converts the first character in the string
$stringto lowercase.
ucfirst($string)
The function converts the first character in the string
$stringto uppercase.
ucwords($string)
The function converts the first character of each word in the string
$stringto uppercase.
PHP String Processing (Jade Girl Heart Sutra Edition)". It's free~ Come and learn!
The above is the detailed content of PHP string learning: Convert the size of the first character (3 methods). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools
