Take question mark off end of string in PHP

I'm new to php, but wasn't able to find a straight answer online, so decided to ask Stack Overflow.

I have a variable, and I want to make a statement similar to the following:

if ($var has a question mark at end)
{
$var = ($var - question mark)
}

Thanks in advance!

--EDIT--

I ended up using the following:

if (substr($var, -1) == "?")
{
$var = rtrim($input, "?");
}

Thanks again for the quick feedback. God I love this site.

--END EDIT--

Answers


Simple answer:

$var = rtrim($var, '?');

hakre is right

$var = rtrim($var, '?');


Just do this:

if ($var[strlen($var) - 1] == "?")
{
    $var = substr($var, 0, strlen($var) - 1);
}

$var = (substr($var,-1) == "?") ? substr($var,0,-1) : $var;

Explanation:

substr($var, -1) gets the last character in your string

then it compares whether that string has question mark or not at the end.

If it has a question mark at the end, it removes it, if it doesn't it leaves the string as is.

The code above is a shorthand version of if(){....} else {} The instructions after the unquoted question mark represent the true block, the instructions after the colon : represent the false block.


What about using strpos function?

example

$mystring = 'abc?';
$findme   = '?';
$pos = strpos($mystring, $findme);

if($pos === true)
{
      //we've found it
}

Need Your Help

what is the event for longpress in vb.net datagridview

vb.net winforms events mouseevent

How can I handle a long press in a cell/row in datagridview in vb.net?