In the previous article, we learned how to change the case of string characters. If necessary, please read "Continue to harm all characters in strings". This time we will introduce to you the method of deleting the characters on the right side of the string. You can refer to it if you need it.
In PHP, there are two ways to remove characters on the right, namely the chop() function and the rtrim() function. First let's look at the first function, chop.
Let’s look at a piece of code first. It’s easier to understand the function based on the code.
<?php $str = "Hello World!"; echo $str . "<br>"; echo chop($str,"World!"); ?>
The result of this is
Let’s take a look at this result. What difference do we find between these two lines? Is it because the second line is one less word "World" than the first line? Does this mean that the function we used successfully deleted the characters on the right? But currently we don’t know how this function works, so should we take a look at its syntax?
chop(要检查的字符串,charlist)
We need to talk about the charlist parameter. This parameter will determine which characters we delete from the string.
When we When you want to remove certain characters, remember to write the corresponding characters. Now that we have introduced the chop function, let’s introduce a particularly commonly used function that most people have heard of, rtrim. Let’s look at a small example first.If the charlist parameter is empty, the following characters are removed:
"\0" - NULL
"\t" - tab character
- ##"\n" - line feed
- "\x0B" - vertical tab character
- "\r" - Enter
- " " - Space
<?php $str = "Hello World!"; echo $str . "<br>"; echo rtrim($str,"World!"); ?>
rtrim(string,charlist)
The above is the detailed content of How to remove characters on the right side of PHP strings. For more information, please follow other related articles on the PHP Chinese website!