$url = new \League\Uri\Uri('https://www.example.com/foo/bar?param=value#fragment'); $host = $url->getHost(); // returns "www.example.com" $path = $url->getPath(); // returns "/foo/bar" $query = $url->getQuery(); // returns "param=value" $fragment = $url->getFragment(); // returns "fragment"
$url = new \League\Uri\Uri('https://www.example.com'); $url = $url->withPath('/foo/bar')->withQuery('param=value')->withFragment('fragment'); echo $url; // outputs "https://www.example.com/foo/bar?param=value#fragment"
$url = new \League\Uri\Uri('https://www.example.com/foo/bar?param=value#fragment'); $url = $url->withHost('api.example.com')->withQuery('newparam=newvalue'); echo $url; // outputs "https://api.example.com/foo/bar?newparam=newvalue#fragment"In these examples, we used the `league/uri` package library to work with URLs in PHP. This library provides a powerful and flexible way to deal with URLs, and is well-documented and actively maintained by the PHP community.