The best solution, in your case, is Dmitry's solution, but you can also use the built-in parse_url function for such purposes, which parses the URL in parts and puts the results into an array.
$link = 'http://site.ru/content/grazhdanskie-spory?clear_cache=Y'; if ($url = parse_url($link)) { printf('%s://%s%s', $url['scheme'], $url['host'], $url['path']); }
Below you asked the question that you want to get the end point of the path in the URL.
I suggest 2 options for solving this problem:
1: parse_url
$url = 'http://site.ru/content/grazhdanskie-spory?clear_cache=Y'; $url_path = parse_url($url, PHP_URL_PATH); $parts = explode('/', $url_path); $last = end($parts); echo $last;
2: basename
$url = "http://site.ru/content/grazhdanskie-spory?clear_cache=Y"; $lastPath = preg_replace("/\?.*/", "", basename($url));
basename will return the endpoint of the path in the URL, but it will be: grazhdanskie-spory? clear_cache = Y in this case, so it needs to be truncated using a regular expression.