Home > Backend Development > PHP Problem > How to remove characters on the right side of PHP strings

How to remove characters on the right side of PHP strings

醉折花枝作酒筹
Release: 2023-03-11 21:36:02
Original
2432 people have browsed it

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

The result of this is

How to remove characters on the right side of PHP strings

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)
Copy after login

We need to talk about the charlist parameter. This parameter will determine which characters we delete from the string.

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

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.

<?php
$str = "Hello World!";
echo $str . "<br>";
echo rtrim($str,"World!");
?>
Copy after login

The result of this is

How to remove characters on the right side of PHP strings

Let’s look at the results of one and two functions. There seems to be no difference. If we look at the code again, it seems that except for the function Apart from being different, there are no other differences. It seems that these two functions are very similar, right? Let's take a look at the syntax of this function.

rtrim(string,charlist)
Copy after login
When you see this, do you think I will write the following? Except for the function name, this function is exactly the same as the chop function. This is indeed the case. Except for the function name, the rtrim function is very similar to the chop function, and can even be said to be exactly the same.

That’s all. If you want to know anything else, you can click here. → →

php video tutorial

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!

Related labels:
php
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