Skip to content
Advertisement

PHP: Check if current URL is part of defined URL

I want to check of the current URL is part of a pre-defined URL.

For example: I want to check if the domain example.com/shop/ is part of the viewed URL.

I’m using the following function to get the current URL and a pre-defined ($shop_page_url) URL:

JavaScript

In this case it only works if the $shop_page_url is the same as the $current_url.

But I want to check if pre-defined URL ($shop_page_url) is part of the current URL (example.com/shop/page/7).

Advertisement

Answer

Is strpos what you’re looking for?

If I’m understanding correctly, you would just want to see if it returns false:

JavaScript

You could also simplify the above if you wanted to save a few lines of code:

JavaScript

strpos() >= 0 won’t work. If the substring is not found at all, doing that will end up returning true, which is not what you’d want here!

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement