Home >Backend Development >PHP Problem >How to delete the end of a string in php

How to delete the end of a string in php

PHPz
PHPzOriginal
2023-03-29 11:33:44530browse

PHP is a powerful programming language that is used in a wide range of fields, including website development, data processing, and application development. In daily development, we often encounter situations where we need to process strings. One of the common operations is to delete characters or blank characters at the end of the string.

In PHP, deleting the trailing whitespace characters and deleting the trailing specified characters are two different operations. Therefore, this article will introduce how to deal with these two situations respectively.

Delete terminal blank characters

Terminal blank characters refer to characters such as spaces, tabs, and newlines at the end of strings. These characters often appear in user-entered data. If they are incorrect String processing will have an impact on the running of the program.

In PHP, we can use the rtrim() function to remove whitespace characters at the end of the string. The sample code is as follows:

$str = "Hello World   \t\n";
$str = rtrim($str);
echo $str;

In the above code, rtrim() The function will remove the whitespace characters at the end of the $str string, and the final output result will be "Hello World", in which spaces, newlines, and tabs will be removed.

Delete the specified characters at the end

If we want to delete the specified characters at the end of the string, such as comma, period, question mark and other symbols, how should we do it? PHP also provides corresponding functions to complete this task. Let's take a look at the specific implementation method.

Method 1: Use the substr() function

First of all, we can use the substr() function that comes with PHP to delete the specified characters at the end of the string. The following example:

$str = "Hello World,";
$str = substr($str, 0, -1);
echo $str;

In the above code, the substr() function will take out all characters from the beginning of the string until the second to last character, and finally return a new string. In this example, we remove the comma from the end of the string and output the result as "Hello World".

Method 2: Use the rtrim() function combined with string length processing

The second method is to combine the rtrim() function mentioned above with PHP characters String length function strlen() is used to remove the specified characters at the end. The code is as follows:

$str = "Hello World,";
$char = ",";
$str = rtrim($str, $char);
echo $str;

Compared with the first method, this method is simpler and easier to understand. In the above code, the rtrim() function will remove all specified characters (comma in this case) from the end of the string, and the final output result will be "Hello World".

Summary

The above is the method for processing terminal characters in PHP. If you need to handle similar string operations in your own program, as long as you master the general methods introduced in this article, This task can be easily accomplished. Hope this article can be helpful to you!

The above is the detailed content of How to delete the end of a string in php. 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