PHP

Remove last character from string


substr
– Return part of a string.

Usage:

substr($str, 0, -1);

Example:

$str = "Hi there.!";
echo $str.'<br>';
echo substr($str, 0, -1).'<br>';

Output:

Hi there.!
Hi there.


mb_substr
– Get part of string – Performs a multi-byte safe substr() operation based on number of characters.

Usage:

mb_substr($str, 0, -1);

Example:

$str = "Hi there.!";
echo $str.'<br>';
echo mb_substr($str, 0, -1).'<br>';

Output:

Hi there.!
Hi there.


substr_replace
– Replace text within a portion of a string.

Usage:

substr_replace($str, '', -1);

Example:

$str = "Hi there.!";
echo $str.'<br>';
echo substr_replace($str, '', -1).'<br>';

Output:

Hi there.!
Hi there.


rtrim
– Strip whitespace (or other characters) from the end of a string.

Usage:

rtrim($str, '!');

Example:

$str = "Hi there.!";
echo $str.'<br>';
echo rtrim($str, '!').'<br>';

Output:

Hi there.!
Hi there.