Download the PHP package filippo-toso/uri without Composer
On this page you can find all versions of the php package filippo-toso/uri. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download filippo-toso/uri
More information about filippo-toso/uri
Files in filippo-toso/uri
Package uri
Short Description A simple class to easily manipulate URLs.
License MIT
Informations about the package uri
URI
A simple class to easily manipulate URLs.
Requirements
- PHP 7.2+
Installing
Use Composer to install it:
Basic usage
You can create an instance of the URI class using the static make()
or through its constructor:
You can also pass other optional parameters that will be used when creating the querystring part of the URL. Check the source code for more details.
Once you have an instance of the class, you can use its fluent API to manipulate the URL as you like.
For instance, let's change the schema and domain:
You can call the following methods to get/set the relative parts of the URL: scheme(), user(), pass(), host(), port(), path(), query() and fragment()
.
For instance, let's get the domain:
The class can be casted to string to get the whole url or you can use the url()
method as shown above.
You can also get the unmodified url using the original()
method.
More complex usage
Now let's do something more complex, for instance, let's change an url through a relative path.
The parameter passed to the relative()
method can be a full URL (in this case the whole URL will be replaced with the new one), an absolute path or a relative path. It can also include the querystring and the fragment.
You can also change only the extension of the file using the extension()
method or replace the querystring using the params()
method (it accepts an array of parameters as input) or the query()
(it accepts a string as input).
Querystring manipulation
Talking about querystring manipulation, there are other useful methods to do that. For instance, you can use:
add()
to add a parameterremove()
to remove a parameterset()
to replace the parameter valueget()
to get the parameter value
All these methods accept a dot notation as the key name. For instance, to change the a parameter like $_GET['post']['content']['html']
you will use the dot notation post.content.html
.
Talking about the remove()
method, instead of a key, you can pass it a callback to remove multiple elements in one go. For instance, here's the code to remove all the utm_*
parameters used to track campaigns in Google Analytics:
That's it, go change the URLs!.