search
HomeBackend DevelopmentPHP ProblemHow to remove characters on the right side of PHP strings

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

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)

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

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)

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version