Remove Last Character from String in PHP

Hello friends,

Here We describes 3 methods to remove last character from a string in PHP programming language. You can use any one methods according to your requirement requirement.

Method 1 – substr_replace function

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

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

Example:

$string = “Hello Webtechsource Admin!”;
echo “String: ” . $string . “\n”;
echo “Output string: ” . substr_replace($string ,””,-1) . “\n”;

Output :

String: Hello Webtechsource Admin!
Output string: Hello Webtechsource Admin

Method 2 – substr function

Use the substr function to remove the last character from any string in PHP string
Syntax:
substr($string, 0, -1);

Example:

$string = “Hello Webtechsource Admin!”;
echo “String: ” . $string . “\n”;
echo “Output string: ” . substr($string, 0, -1) . “\n”;

Output:
String: Hello Webtechsource Admin!
Output string: Hello Webtechsource Admin

Method 3 – rtrim function
The rtrim function to used to remove specific characters from the end of the string.

Syntax:
rtrim($string,’x’);
Here “x” is the character to remove.

Example:

$string = “Hello Webtechsource Admin!”;
echo “string: ” . $string . “\n”;
echo “Output string: ” . rtrim($string, “!”) . “\n”;

Output:
String: Hello Webtechsource Admin!
Output string: Hello Webtechsource Admin

 

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – we are here to solve your problems.

I, Dayanand Saini,the founder and chief editor of www.webtechsource.com,have been working as a software engineer from last 4+ years. It's my hobby to learn new things and implement on live environment.

Leave a Reply

Your email address will not be published. Required fields are marked *

12 − = 3

%d bloggers like this: