How to remove last character of string using php

不言
Release: 2023-04-05 09:28:01
Original
6657 people have browsed it

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

The example is as follows

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

The running effect is as follows:

Original string: Hello admin!
Updated string: Hello admin
Copy after login

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

The example is as follows

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

Run The effect is as follows:

Original string: Hello Admin!
Updated string: Hello Admin
Copy after login

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

The example is as follows

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

The running effect is as follows

Original string: Hello Admin!
Updated string: Hello Admin
Copy after login

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

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

The running result is as follows

Original string: Hello Admin!
Updated string: Hello Admin
Copy after login

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!

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