I have to check if a $url equals to a string with an optional part:
if ($url === 'http://www.example.com/login/') {}
Where the trailing slash "/" at the end of "login" is optional.
How to make it return TRUE with or without the trailing slash?
try using the stripos function:
if(stripos($url,'http://www.example.com/login') !== false){}
Note: used stripos (case insensitive strpos() ) in case the URL is spelled with uppercase letters
http://www.example.com/login would also match.User regular expression:
if(preg_match("@http:\/\/www\.example\.com\/login\/?)$@") == 1)
== 1 part1 will always evaluate to true and 0 to false. If it returns an error, it returns false too, so if you don't want to know if it didn't work or if it didn't work because of an error, then no, == 1 is very not necessary at all, and yes, just avoiding it IS checking the (casted to boolean) return value. If you really want to use it, then I would prefer === 1You could use rtrim() function in this case :
if (rtrim($url, '/') === 'http://www.example.com/login') {
}
if ($url === 'http://www.example.com/login/' || $url === 'http://www.example.com/login') {}
|| means Or so if $url is equal to http://www.example.com/login/ OR $url is equal to http://www.example.com/login
Source: http://php.net/manual/en/language.operators.logical.php
strposis what you looking for