Remove question mark from end of string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
noisymouse
Forum Newbie
Posts: 3
Joined: Mon Dec 14, 2009 3:50 pm

Remove question mark from end of string

Post 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.
iamngk
Forum Commoner
Posts: 50
Joined: Mon Jun 29, 2009 2:20 am

Re: Remove question mark from end of string

Post by iamngk »

You can make things easy by using rtrim() function
For example: $tempSentence = rtrim($temtempSentence,'?');
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Remove question mark from end of string

Post 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);
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Remove question mark from end of string

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Remove question mark from end of string

Post 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().
Post Reply