Code: Select all
if ($tempSentence[strlen($temtempSentence)-1] == '?')
{
$tempSentence = substr($tempSentence,0,-1);
}Moderator: General Moderators
Code: Select all
if ($tempSentence[strlen($temtempSentence)-1] == '?')
{
$tempSentence = substr($tempSentence,0,-1);
}For example: $tempSentence = rtrim($temtempSentence,'?');
Code: Select all
$tempSentence = substr($tempSentence,0,strlen($tempSentence)-1);Actually, using -1 is okay. It's a special thing PHP supports. -X means "go up to the end of the string except the last X characters". Something similar for the start offset too.califdon wrote:The 3rd argument in the call to substr() is not the position of the end, it is the length of the string you want. So you have to take the length of the string and subtract one from it:Code: Select all
$tempSentence = substr($tempSentence,0,strlen($tempSentence)-1);
start
If start is non-negative, the returned string will start at the start 'th position in string , counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.
If start is negative, the returned string will start at the start 'th character from the end of string .
If string is less than or equal to start characters long, FALSE will be returned.
length
If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string ).
If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes a position beyond this truncation, an empty string will be returned.
If length is given and is 0, FALSE or NULL an empty string will be returned.
Code: Select all
echo substr("123456789", 0, -1); // 12345678