0

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?

2
  • I know this but I remember I came across an example looking like a regex without using OR. Commented Jun 19, 2014 at 14:41
  • 2
    strpos is what you looking for Commented Jun 19, 2014 at 14:41

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Remove the trailing slash from your string if you want it to be optional. Btw, any other URL containing http://www.example.com/login would also match.
I was not payiing attention to that trailing ' / ' :) I edited it :)
2

User regular expression:

if(preg_match("@http:\/\/www\.example\.com\/login\/?)$@") == 1)

4 Comments

Good solution but you dont need the == 1 part
You don't necessarily need it to work as expected, but it's still OK to check for return value isn't it.
in a boolean condition, 1 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 === 1
@Capsule I second everything you said, but I want to add that in our company we have a policy of explicitly writing checks after library functions for the purpose of instantly understanding what check is performed. It's a question of clean-code. While it creates a tiny overhead by adding a check to the function in question, it is much more readable, and processor time for a simple check is nothing that needs micro optimization. The time a coworker needs to understand functions he doesn't know can rather be spent implementing time-consuming tasks like big database queries
1

You could use rtrim() function in this case :

if (rtrim($url, '/') === 'http://www.example.com/login') {
}

Demo without trailing slash
Demo with trailing slash

5 Comments

Wouldn't this also trim the slashes after http and after .com etc? What he could do is also rtrim the $url
Take a second look at that
Yeah thanks, I forgot to remove the trailing slash in the test case the first time.
@Brewal I'm sorry, I had a small brain failure. Entschuldigung.
you should be the one to take a second look at what you've just done
0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.