136

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'.

Krish Munot
  • 1,093
  • 2
  • 18
  • 29
pisoe
  • 1,369
  • 2
  • 9
  • 3
  • http://www.php.net/manual/en/function.strpos.php – teemitzitrone Aug 19 '11 at 08: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 – Matt Aug 19 '11 at 08:31
  • You might find [`s($_SERVER['REQUEST_URI'])->contains('car')`](https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str.php#L93) helpful, as found in [this standalone library](https://github.com/delight-im/PHP-Str). – caw Jul 27 '16 at 00:41
  • What if the url contains the word "scared"? Should that print "car is exist"? If not, most of the answers here are wrong. – doug65536 Oct 14 '16 at 07:10

12 Answers12

260

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.';
}
aug
  • 11,138
  • 9
  • 72
  • 93
digi
  • 2,719
  • 1
  • 14
  • 17
  • 1
    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! – Javacadabra Nov 29 '16 at 13:06
  • `$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? – Si8 Feb 21 '18 at 16:51
  • From PHP 8.0 onwards, you can also use `str_contains($url, 'car')`: https://www.php.net/manual/en/function.str-contains.php – Filnor May 10 '22 at 07:58
91

I think the easiest way is:

if (strpos($_SERVER['REQUEST_URI'], "car") !== false){
// car found
}
SohailRajput
  • 629
  • 1
  • 7
  • 18
santillanix
  • 1,947
  • 18
  • 17
  • Perfect... I needed to match http://example.com/events/pagename to find any urls with "events" in them and it works! – Tingo Feb 16 '18 at 02:19
  • I've used this several times in the past week on 4 different projects. Simple. Thanks. – Pegues Nov 02 '18 at 01:47
  • This does not test the full url; only the path. It will not test against the scheme, domain, port, query parameters, or fragment. – kloddant Aug 02 '19 at 15:50
25
$url = " www.domain.com/car/audi/";
if (strpos($url, "car")!==false){
    echo "Car here";
}
else {
   echo "No car here :(";
}

See strpos manual

J0HN
  • 26,063
  • 5
  • 54
  • 85
16
if( strpos( $url, $word ) !== false ) {
    // Do something
}
nobody
  • 10,599
  • 4
  • 26
  • 43
12

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

Mars
  • 131
  • 1
  • 6
11

worked for me with php

if(strpos($_SERVER['REQUEST_URI'], 'shop.php') !== false){
echo 'url contains shop';
}
Shuhad zaman
  • 3,156
  • 32
  • 32
  • 3
    This is essentially identical to [this three your old answer](https://stackoverflow.com/a/28875202/466862). Why bother posting an answer like that? – Mark Rotteveel Aug 18 '18 at 09:42
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?

Mathijs Segers
  • 6,168
  • 9
  • 51
  • 75
3

Have a look at the strpos function:

if(false !== strpos($url,'car')) {
    echo 'Car exists!';
}
else {
    echo 'No cars.';
}
teemitzitrone
  • 2,250
  • 17
  • 15
Jeff
  • 6,643
  • 3
  • 25
  • 35
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...

Mikeys4u
  • 1,494
  • 18
  • 26
2

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';
}
Zombo
  • 1
  • 62
  • 391
  • 407
1
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];


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

This seems to work.

Richard Andrew Lee
  • 1,187
  • 6
  • 10
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..

chris
  • 36,115
  • 52
  • 143
  • 252