Remove Last Character From String in Php
Remove Last Character From String in Php : We sometimes need to remove last character from string such as comma(,), semicolon(;) etc. We can remove last character from string in php using the function substr(). There are many ways to remove last character from string we are going to explain some of them with example and demo.
Remove Last Character From String in Php
You can remove the last character using the following – method 1
Remove Last Character From String Using substr() Method 1
Using the substr() function we can remove the last character as
Remove Last Character From String in Php: Remove Last Comma(,)
<?php $str = "Test,"; $output = substr($str, 0, -1); echo $output; ?> |
If you run the above example it will produce the following output –
Test
Remove Last Character From String Using rtrim() – Method 2
Using the rtrim() function we can remove the last character as
Remove Last Character From String in Php: Remove Last Comma(,)
<?php $str = "Test,"; $output = rtrim($str, ","); echo $output; ?> |
rtrim($str, “,”) – first parameter is string and second parameter is character you want to remove. rtrim() function removes the character from the right of string if the character is specified otherwise it will remove blank space if found. If you run the above example it will produce the following output –
Test
Advertisements