Possible Duplicate:
remove last character from string
I want to delete the last character in the text..
For example
My string: example text
Second : example tex
How?
Possible Duplicate:
remove last character from string
I want to delete the last character in the text..
For example
My string: example text
Second : example tex
How?
Use substr
$text = 'Example Text';
$text = substr($text, 0, -1)
substr takes three arguments, the original text, start and length, it return the substring from the original text starting at start with the given length. If length is negative, that number of characters will be excluded from the string, that's why whe are using the value -1, to exclude the last char from the string.
Use substr
, which gets a substring of the original string:
$str = 'example text';
$newStr = substr($str, 0, -1); // "example text"
0
means "start at the beginning", -1
means "go until one character before the end".
Simple and straight to the point
$str[strlen($str) - 1] = '';
Does your stuff.
Strings are in actuality an array of characters in php. You can therefore set the last index to an empty string.
Using unset()
would have been nicer but php doesn't allow unsetting string indices.