141

I would like to know if some word is present in the URL.

For example, if word car is in the URL, like www.domain.com/car/ or www.domain.com/car/audi/ it would echo 'car is exist' and if there's nothing it would echo 'no cars'.

4
  • php.net/manual/en/function.strpos.php Commented Aug 19, 2011 at 8:27
  • This also depends if you just looking for the word car or if you also want the type of car or cars too. You might need preg_match, strpos, explode and in_array/array_search, really just depends. if you want something simple, just use strpos as suggested Commented Aug 19, 2011 at 8:31
  • You might find s($_SERVER['REQUEST_URI'])->contains('car') helpful, as found in this standalone library. Commented Jul 27, 2016 at 0:41
  • What if the url contains the word "scared"? Should that print "car is exist"? If not, most of the answers here are wrong. Commented Oct 14, 2016 at 7:10

12 Answers 12

265

Try something like this. The first row builds your URL and the rest check if it contains the word "car".

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


if (strpos($url,'car') !== false) {
    echo 'Car exists.';
} else {
    echo 'No cars.';
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is a good solution but just be wary that this will match a URL that has car anywhere. For example www.domain.com/car-pricing or www.domain.com/carparks will validate and output Car exists. Maybe it doesn't matter in your case but for others it might be relevant!
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; echo count(strpos($url,'category')); gives me 1 regardless of whether category exist in the Url or not. Any idea why?
From PHP 8.0 onwards, you can also use str_contains($url, 'car'): php.net/manual/en/function.str-contains.php
93

I think the easiest way is:

if (strpos($_SERVER['REQUEST_URI'], "car") !== false){
// car found
}

3 Comments

Perfect... I needed to match example.com/events/pagename to find any urls with "events" in them and it works!
I've used this several times in the past week on 4 different projects. Simple. Thanks.
This does not test the full url; only the path. It will not test against the scheme, domain, port, query parameters, or fragment.
25
$url = " www.domain.com/car/audi/";
if (strpos($url, "car")!==false){
    echo "Car here";
}
else {
   echo "No car here :(";
}

See strpos manual

Comments

16
if( strpos( $url, $word ) !== false ) {
    // Do something
}

Comments

13

This worked for me:

// Check if URL contains the word "car" or "CAR"
   if (stripos($_SERVER['REQUEST_URI'], 'car' )!==false){
   echo "Car here";
   } else {
   echo "No car here";
   }
If you want to use HTML in the echo, be sure to use ' ' instead of " ".
I use this code to show an alert on my webpage https://geaskb.nl/ 
where the URL contains the word "Omnik" 
but hide the alert on pages that do not contain the word "Omnik" in the URL.

Explanation stripos : https://www.php.net/manual/en/function.stripos

Comments

11

worked for me with php

if(strpos($_SERVER['REQUEST_URI'], 'shop.php') !== false){
echo 'url contains shop';
}

1 Comment

This is essentially identical to this three your old answer. Why bother posting an answer like that?
4

strstr didn't exist back then?

if(strstr($_SERVER['REQUEST_URI'], "car")) {
   echo "car found";
}

This must be one of the easiest methods right?

Comments

3

Have a look at the strpos function:

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

Comments

3

Starting with PHP 8 (2020-11-24), you can use str_contains:

if (str_contains('www.domain.com/car/', 'car')) {
   echo 'car is exist';
} else {
   echo 'no cars';
}

1 Comment

I upvoted this answer because str_contains is a name that describes better what the function does than strpos and str_contains works perfect. I would like to add that I have tested both functions and in my environment str_contains is on average 3-4 times slower than strpos. It is all a matter of a few milliseconds but in situations that need performance I would use strpos.
2

Surely this is the correct way round....

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


if (!strpos($url,'mysql')) {
echo 'No mysql.'; //swapped with other echo statement
} else {
echo 'Mysql exists.';
}

Otherwise its reporting the opposite way it should...

Comments

1

You can try an .htaccess method similar to the concept of how wordpress works.

Reference: http://monkeytooth.net/2010/12/htaccess-php-how-to-wordpress-slugs/

But I'm not sure if thats what your looking for exactly per say..

Comments

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


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

This seems to work.

1 Comment

Wrong way around here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.