URLs manipulation in PHP (the easy way)

Filippo Toso
3 min readMar 13, 2022
Change your URLs with a simple fluent PHP class

I work often on web scrapers and, in this line of work, I need a lot of URLs manipulations.

So I wrote a small PHP class that allows me to do that in a very fluent and easy way (if I may say so).

You can install it using composer:

composer require filippo-toso/uri

You can create an instance of the URI class using the static make() or through its constructor:

use FilippoToso\URI\URI;$url = 'http://www.example.com/dir/sub/file.php?name=john&emailjohn@smith.com#fragment';$uri = URI::make($url);// or$uri = new URI($url);

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:

use FilippoToso\URI\URI;$url = 'http://www.example.com/dir/sub/file.php?name=john&emailjohn@smith.com';$newUrl = URI::make($url)
->scheme('https')
->domain('test.com')
->url();

--

--

Filippo Toso

Filippo is a software developer who helps medium businesses automate and optimize their internal processes through AI-powered interactive bots.