Remove the Last Character From a PHP String

Removes the last character from a string, in the example below it is a comma. The last character can be a return character or tab or any other character.

$array = array_splice($array, $n, 0, $item);

Example 1:

String
$string = "This is a string with an unwanted character at the end of it..";

PHP:

$string = rtrim($string, '.');

Result:
"This is a string with an unwanted character at the end of it."


Example 2:

 // in the string below notice the return which forces the quote to the next line.
$string = "This is a string with an unwanted character at the end of it.
";

PHP:

$string = rtrim($string, PHP_EOL);

Result:
"This is a string with an unwanted character at the end of it."

Copyright © Lage.us Website Development | Disclaimer | Privacy Policy | Terms of Use