All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Remove Last Character From String PHP

Last Updated : Mar 11, 2024

Remove Last Character From String PHP

In this tutorial we will show you the solution of remove last character from string PHP, today we are going to understand how to remove last character from a string in php.

Sometimes by mistake we add characters that are not required. There are many method with help of which we can remove them.

But today we are going to understand the removal of c character from string in php at last location. So, let us see how to done this.

Step By Step Guide On Remove Last Character From String PHP :-

Here, as we say that we can remove a character from any location in php, for this we have to use trim() function there.

And to remove last character there is a function related to trim() that is rtrim().

This rtrim() removes predefined characters and whitespace from right side of string, means it helps us to remove last character.

<!DOCTYPE html>
<html>
<title>
 Remove last character from string php
</title>
<body>
<?php
$str = "Welcome to Talkerscode World!";
echo $str . "<br>";
echo rtrim($str,"World!");
// another example
$ arrayname = this, is , another, example, here,
$newarray = rtrim($arrayname, ", ");
echo $newarray;
?>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  6. Here, as you can see that we give two examples there. In first as you see, we store a string into a variable and then echo that string to show what the variable contains.
  7. Now, if we want to remove last word or also last character from the string stored in variable. Then just use rtrim() function and inside this we have to provide the string from which we want to remove last character and the character which we want to remove.
  8. In second example, as we see that we create a string and install them in a variable.
  9. And we want to remove a character (,) that is at last so, we apply rtrim() to that variable and specify the character that we want to remove.
  10. We know that rtrim is used to remove last character from right side. Hence, the last (,) is removed.
  11. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to remove last character from string in php.

I hope this tutorial on remove last character from string PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪