Do not show code if URL contains phrasses

Hello,

i have an javascript which i want to show only if URL do NOT contains certain phrasses. Like include javascript in webpage only if URL is free from words: cars, motorcycles

So far i found this code, but it is only for one word:

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];


if (false !== strpos($url,'car')) {
    echo 'Car exists.';
} else {
    echo 'No cars.';
}

Do you know how to use loops? Have all of your terms in an array, and do a foreach loop through the array. On each item, say if (the array item is present in url) then ( make a counter variable like $failed =1 and break)

Then you can use a simple conditional
if (failed) then …

else…

Edit: The above assumes you want a solution that will let you have several terms. If you only have a few, you could easily do an if / else if / else exactly as you have it - or a switch statement.

Instead of using strpos() you could use preg_match() with a regular expression that defines all of the valuse that you are looking for.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.