Home  >  Article  >  Backend Development  >  How to convert the last few characters of a specified string to uppercase in PHP and leave the rest unchanged

How to convert the last few characters of a specified string to uppercase in PHP and leave the rest unchanged

藏色散人
藏色散人Original
2021-08-12 09:48:322505browse

In the previous article "How does PHP determine whether the sum of two numbers is 30 or whether a certain number is 30" I will introduce to you how to determine two numbers. I believe everyone is familiar with the PHP involved. Now that you have a certain grasp of operators, this article will continue to introduce you to a basic question "How to convert the last few characters of a specified string to uppercase".

First let us fully describe the central question of this article: "Please write a PHP program to convert the last 3 characters of a given string to uppercase; if the length of the string is less than 3, then capitalize all characters".

Regarding converting case, I think everyone immediately thought of the two PHP built-in functions strtolower() and strtoupper(), but we need to implement the conversion according to the requirements in the question.

Now I will take you to introduce the implementation method in detail:

The complete PHP code is as follows:

<?php
function test($s)
{
    return strlen($s) < 3 ? strtoupper($s) : substr($s, 0, strlen($s) - 3).strtoupper(substr($s, strlen($s) - 3));
}


echo test("Python")."<br>";
echo test("Javascript")."<br>";
echo test("js")."<br>";
echo test("PHP");

The output result is as follows:

How to convert the last few characters of a specified string to uppercase in PHP and leave the rest unchanged

It’s obvious!

So as you can see from the above code, are there any knowledge points that we need to master?

The first is the strlen() and substr() functions. The strlen function is used to return the length of the string, and the substr() function returns a part of the string.

Then there is the strtoupper() function, which is used to convert the string to uppercase.

→ Attached related functions:

lcfirst() function: Convert the first character in the string to lowercase;

strtolower() function: Convert the string to lowercase;

ucfirst() function: Convert the first character in the string to uppercase;

ucwords() function: Convert the first character of each word in the string to uppercase;

Then there is the use of the ternary operator "?:", when in front of the question mark When the expression is true, the expression before the colon is executed, otherwise the expression after the colon is executed.

OK, I believe everyone has mastered it!

Finally, I would like to recommend the latest and most comprehensive "PHP Video Tutorial"~ Come and learn!

The above is the detailed content of How to convert the last few characters of a specified string to uppercase in PHP and leave the rest unchanged. 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