Skip to content
PHP Logo

How can PHP be used to check for a specific string in a URL path?

To check for a specific string in a URL path using PHP, you can use the `$_SERVER['REQUEST_URI']` variable to get the current URL path and then use the `strpos()` function to check if the specific string exists in the URL path.

php
$url = $_SERVER['REQUEST_URI'];
$specificString = 'example';

if(strpos($url, $specificString) !== false){
    echo 'The specific string is present in the URL path.';
} else {
    echo 'The specific string is not present in the URL path.';
}