PHP:检查URL中给定字符串后是否有任何内容?

I need to create a an if statement that fires if there is something after a given string in the URL.

For example, if my string is 'Honda' I need to check if something appears after that word in the url, eg something like:

$brand = "honda";

if (url contains $brand . '/*'){
  // Do something.
}

Example urls could be:

  • mysite.com/honda (which would fail the above)
  • mysite.com/cars/honda (which would fail the above)
  • mysite.com/honda/civic (which would pass the above)
  • mysite.com/honda/accord (which would pass the above)

I know I can use strpos() to detect if the string is within the URL, but how can I detect if anything comes after that?

Use a regular expression that looks for the $brand at the end of the string.

if (! preg_match("/{$brand}$/", $url)) {
    // ...
}

If you need to check if the $brand actually appears in the URL before running your end of string check:

if (strpos($brand, $url) !== false && ! preg_match("/{$brand}$/", $url)) {
    // ...
}