storeManager = $storeManager; } /** * Remove base url from giving url. * * @param string $url * @param bool $removeGetParams * @param bool $removeFragment * @return string */ public function execute(string $url, bool $removeGetParams = true, bool $removeFragment = true): string { $baseUrl = $this->storeManager->getStore()->getBaseUrl(); $url = str_replace(["\n", "\r"], '', $url); if ($removeGetParams && $this->hasParams($url)) { $paramsWithFragment = strstr($url, '?'); if ($removeFragment) { $url = str_replace($paramsWithFragment, '', $url); } else { $url = str_replace(strstr($paramsWithFragment, '#', true), '', $url); } } if ($removeFragment && $this->hasFragment($url)) { $url = str_replace(strstr($url, '#'), '', $url); } $url = str_replace($baseUrl, '', $url); if ('' === $url || $url[0] !== '/') { $url = '/' . $url; } return $url; } /** * Check if url has get params. * * @param string $url * @return bool */ private function hasParams(string $url): bool { return strpos($url, '?') !== false; } /** * Check if url has fragment (hash). * * @param string $url * @return bool */ private function hasFragment(string $url): bool { return strpos($url, '#') !== false; } }