How do I remove the last character only if it's a period?
$string = "something here."; $output = 'something here';
The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.
To remove the last three characters from a string, we can use the substr() function by passing the start and length as arguments.
To remove the last three characters from the string you can use string. Substring(Int32, Int32) and give it the starting index 0 and end index three less than the string length. It will get the substring before last three characters.
$output = rtrim($string, '.');
(Reference: rtrim on PHP.net)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With