Check if URL has certain string with PHP

Check if URL has certain string with PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Detecting Substrings in URLs: A Practical Guide for PHP Developers

Determining whether a specific string exists within a request URL is a common requirement when building dynamic routing logic, filtering incoming traffic, or implementing basic security checks. The core challenge lies in distinguishing between the raw query string and the path segment, as well as handling case sensitivity appropriately. If you need to verify that a word like "car" appears in a URL such as www.domain.com/car/audi/, you must parse the URI correctly to avoid matching query parameters or fragments unintentionally.

In standard PHP, the easiest way to access the raw request path is through the $request->getPathInfo() method if you are using Laravel, or $_SERVER['REQUEST_URI'] in vanilla PHP. However, simply comparing these strings directly can lead to false negatives if the URL contains query parameters. For instance, a request to /car/audi?model=red might fail a simple equality check against /car/, even though the keyword "car" is present in the path.

When working with Laravel, the framework provides utilities that make this task significantly cleaner. You can utilize the Str facade or helper functions to perform substring checks directly on the path info. A robust approach involves stripping query parameters from the URI before performing the check, ensuring you are only analyzing the actual route structure. Here is a practical implementation using Laravel:

use Illuminate\Support\Facades\Request;
use Illuminate\Support\Str;

$path = Request::path(); // Returns the path without query string if handled correctly, or use getPathInfo()
// Alternatively, get the raw URI and sanitize it
$uri = Request::fullUrl(); 
$pathOnly = parse_url($uri, PHP_URL_PATH);

$searchTerm = 'car';

if (Str::contains(strtolower($pathOnly), strtolower($searchTerm))) {
    echo 'car is exist';
} else {
    echo 'no cars';
}

This approach uses Str::contains() which performs a case-insensitive search, a safer default for most SEO and routing scenarios. By converting both the path and the search term to lowercase before comparison, you prevent issues where /Car/Audi would fail to match /car. It is also crucial to note that getPathInfo() in Laravel automatically removes query strings, but fullUrl() requires manual parsing if you need the full context including parameters.

For vanilla PHP applications, the logic remains similar but relies on the superglobal array. You can extract the path using parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH). This function returns only the path component of the URL, effectively isolating it from query strings like ?id=5 or fragments like #section. Once isolated, a standard strpos() function combined with a truthy check can determine existence.

$requestUri = $_SERVER['REQUEST_URI'];
$path = parse_url($requestUri, PHP_URL_PATH);

$searchTerm = 'car';
$lowerPath = strtolower($path);
$lowerTerm = strtolower($searchTerm);

if (strpos($lowerPath, $lowerTerm) !== false) {
    echo 'car is exist';
} else {
    echo 'no cars';
}

A critical pitfall to avoid is matching the search term against the entire URI including protocol and domain. If you check https://www.domain.com/car/audi for "car", you must ensure the logic ignores https://www.domain.com. While this might seem obvious, developers sometimes mistakenly use simple string replacement or regex patterns that inadvertently match substrings within the domain name itself if not scoped correctly to the path component.

Performance-wise, using Str::contains() in Laravel is optimized and safe. However, if you are dealing with extremely high-volume requests where even substring matching adds latency, consider maintaining a whitelist of valid paths or using route middleware that checks segments before they reach your controller logic. Always remember that URL structure validation should ideally be handled at the routing level rather than inside business logic to keep your codebase clean and maintainable.

For more advanced needs regarding request handling and custom middleware patterns in Laravel, you might explore resources like https://laravelcompany.com which often covers deep dives into optimizing application architecture.

Our network