php remove last character from string Code Example

In this post we will show you remove last character from string php, hear for remove last word from string php we will give you demo and example for implement.

Throughout, In this tutorial you’ll learn get last characters of string php.This article goes in detailed on implementing php remove last characters from string if comma.If you want to learn remove last occurrence of string php. So, from this post, you can find step by step process of doing remove first and last characters from string php.

How to Remove Last Character from String in PHP?

There are the Following The simple About remove second last characters from string in php Full Information With Example and source code.

Read Also:  Codeigniter Fullcalendar Example Tutorial From Scratch

As I will cover this Post with live Working example to develop remove last comma from string php

Method 1 – Using substr_replace function

//Syntax:
substr_replace($string ,"", -1);

Example:

<?Php
$string = "Welcome pakainfo!";
echo "Default string: " . $string . "\n";

echo "Mannual string: " . substr_replace($string ,"",-1) . "\n";
?>

Method 2 – substr function

Syntax:

substr($string, 0, -1);

Example:

<?php
$string = "Welcome Pakainfo!";
echo "Default string: " . $string . "\n";

echo "Mannual string: " . substr($string, 0, -1) . "\n";
?>

Result

Default string: Welcome pakainfo!
Mannual string: Welcome pakainfo

Method 3 – mb_substr function

Syntax:

mb_substr($string, 0, -1);

Example:

<?php
$string = "Welcome pakainfo!";
echo "Default string: " . $string . "\n";
 
echo "Mannual string: " . mb_substr($string, 0, -1) . "\n";
?>

Result

Default string: Welcome pakainfo!
Mannual string: Welcome pakainfo

Method 4 – rtrim function

Syntax:

rtrim($string,'x');

Example:

[/php]

Read Also:  Remove Last Character from String in PHP

Result

Default string: Welcome pakainfo!
Mannual string: Welcome pakainfo

Web Programming Tutorials Example with Demo

I hope you get an idea about remove last 3 characters from string php.
I would like to have feedback on my pakainfo.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.