Remove trailing delimiting character from a delimited string

To remove the trailing delimiter from a delimited string in PHP, you can use the rtrim function. This function removes characters from the right side of a string.

For example, consider the following string:

$string = "apple,banana,orange,";

To remove the trailing comma from this string, you can use the following code:

<?php

$string = "apple,banana,orange,";
echo $string = rtrim($string, ",");

Watch a course Learn object oriented PHP

This will remove the trailing comma from the string, resulting in the following string:

"apple,banana,orange"

You can also use the rtrim function to remove multiple characters from the right side of a string. For example, to remove both the comma and the space from the end of the string, you can use the following code:

<?php

$string = "apple,banana,orange,";
echo $string = rtrim($string, ", ");

You can also use the substr function to remove the last character from a string. For example, to remove the last character from the string, you can use the following code:

<?php

$string = "apple,banana,orange,";
echo $string = substr($string, 0, -1);

This will remove the last character from the string, resulting in the same string as before:

"apple,banana,orange"