0

I've tried the following:

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


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

However if the url contains the word "care" or "car2" it will also trigger "Car Exists". How do I get a match if only "car" is found?

cormac
  • 11
  • 1

1 Answers1

0

Change

if (strpos($url,'car') !== false) {

into

if (preg_match('/\bcar\b', $url) !== 0) {

Basically, you search for word car with no other alphanumeric surrounding it.


Hope this helps!

srifqi
  • 71
  • 1
  • 6