PHP string learning: Convert the size of the first character (3 methods)

青灯夜游
Release: 2023-03-12 07:58:01
Original
2188 people have browsed it

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

Output result:

PHP string learning: Convert the size of the first character (3 methods)

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 the strtolower($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 the strtoupper($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");
?>
Copy after login

Output result:

PHP string learning: Convert the size of the first character (3 methods)

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 $string to lowercase.

  • strtoupper($string)The function converts all characters in the string $string to uppercase.

  • lcfirst($string)The function converts the first character in the string $string to lowercase.

  • ucfirst($string)The function converts the first character in the string $string to uppercase.

  • ucwords($string)The function converts the first character of each word in the string $string to uppercase.

Finally, I would like to recommend a classic course on our platform "

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!