Home >Backend Development >PHP Tutorial >How to remove last character of string using php

How to remove last character of string using php

不言
不言Original
2019-02-16 11:06:546680browse

There are many ways to delete the last character of a string in How to remove last character of string using php. This article will introduce to you four methods of deleting the last character of a string in How to remove last character of string using php. Friends in need can refer to it.

How to remove last character of string using php

Method 1: substr_replace function

Use the substr_replace function to remove the last character from a string in PHP.

The basic syntax is as follows:

substr_replace($string ,"", -1);

The example is as follows

$string = "Hello admin!";
echo "Original string: " . $string . "\n";
echo "Updated string: " . substr_replace($string ,"",-1) . "\n";

The running effect is as follows:

Original string: Hello admin!
Updated string: Hello admin

Method 2: substr function

Use the substr function to remove the last character from any string in PHP string

The basic syntax is as follows

substr($string, 0, -1);

The example is as follows

$string = "Hello Admin!";
echo "Original string: " . $string . "\n";
echo "Updated string: " . substr($string, 0, -1) . "\n";

Run The effect is as follows:

Original string: Hello admin!
Updated string: Hello admin

Method 3: mb_substr function

Use the mb_substr function to delete characters from the end of the string.

The basic syntax is as follows

mb_substr($string, 0, -1);

The example is as follows

$string = "Hello Admin!";
echo "Original string: " . $string . "\n";
echo "Updated string: " . mb_substr($string, 0, -1) . "\n";

The running effect is as follows

Original string: Hello Admin!
Updated string: Hello Admin

Method 4: rtrim function

rtrim function is used to delete specific characters from the end of a string

The basic syntax is as follows

rtrim($string,'x');

Here "x" is the character to be deleted.

The example is as follows

$string = "Hello TecAdmin!";
echo "Original string: " . $string . "\n";
echo "Updated string: " . rtrim($string, "!") . "\n";

The running result is as follows

Original string: Hello Admin!
Updated string: Hello Admin

This article ends here. For more exciting content, you can pay attention to How to remove last character of string using php Chinese Other related column tutorials on the Internet! ! !

The above is the detailed content of How to remove last character of string using 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
Previous article:How to use feof functionNext article:How to use feof function