PHP code example of bermudaphp / url

1. Go to this page and download the library: Download bermudaphp/url library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bermudaphp / url example snippets


$url = Url::parse('http://username:password@hostname:9090/path?arg=value#anchor');

^ Bermuda\Url\Url {#231 ▼
  -segments: array:8 [▼
    "scheme" => "http"
    "host" => "hostname"
    "port" => 9090
    "user" => "username"
    "pass" => "password"
    "path" => "/path"
    "query" => "arg=value"
    "fragment" => "anchor"
  ]
}

$url->without('fragment', 'path')->toString(); // "http://username:password@hostname:9090/?arg=value"

$currentUrl = Url::fromGlobals()->toString(); // https://github.com/bermudaphp/url
$currentUrl = $currentUrl->withHost('new-hostname.com');

$currentUrl->host; // 'new-hostname.com'

$currentUrl->toString(); // 'https://new-hostname.com/bermudaphp/url'
$currentUrl->toArray();

^ array:1 [▼
  "segments" => array:3 [▼
    "scheme" => "https"
    "host" => "new-hostname.com"
    "path" => "/bermudaphp/url"
  ]
]

Url::build(['scheme' => 'https', 'host' => 'github.com', 'path' => 'bermudaphp/url']); // "https://github.com/bermudaphp/url"