Page 1 of 1

Remove question mark from end of string

Posted: Mon Dec 14, 2009 10:10 pm
by noisymouse
I'm trying to do something relatively simple: a question mark from the end of a string. My code to accomplish this is :

Code: Select all

if ($tempSentence[strlen($temtempSentence)-1] == '?')
  {
    $tempSentence = substr($tempSentence,0,-1);
  }
But this does not seem to work. Any ideas why? Thanks.

Re: Remove question mark from end of string

Posted: Mon Dec 14, 2009 10:28 pm
by iamngk
You can make things easy by using rtrim() function
For example: $tempSentence = rtrim($temtempSentence,'?');

Re: Remove question mark from end of string

Posted: Mon Dec 14, 2009 10:30 pm
by califdon
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);

Re: Remove question mark from end of string

Posted: Mon Dec 14, 2009 11:01 pm
by requinix
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);
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.
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
I suspect OP's problems are due to the fact that he misspelled the variable.

Re: Remove question mark from end of string

Posted: Tue Dec 15, 2009 11:49 am
by califdon
Thanks, tasairis, that's something I didn't know. Just goes to show you that the very first thing to do is RTFM! And good eye, there, too. I missed the misspelling, too.

To the OP: fix your spelling, study the manual, and note that there are several ways to do this, as there are most things in PHP. In this case, you could use substr(), rtrim(), or egrep().