PHP code example of basteyy / url-scheme-validator

1. Go to this page and download the library: Download basteyy/url-scheme-validator 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/ */

    

basteyy / url-scheme-validator example snippets



$validator = new \basteyy\UrlSchemeValidator\UrlSchemeValidator('http://www.example.com');
echo $validator->getScheme(); // Returns http


$validator = new \basteyy\UrlSchemeValidator\UrlSchemeValidator('//www.example.com');
echo $validator->getScheme(); // Returns http
echo $validator->getUrl(); // Returns http://www.example.com


$validator = new \basteyy\UrlSchemeValidator\UrlSchemeValidator('//www.example.com:443');
echo $validator->getScheme(); // Returns https
echo $validator->getUrl(); // Returns https://www.example.com


$validator = new \basteyy\UrlSchemeValidator\UrlSchemeValidator;
$validator->setUrl('www.example.com');
$validator->setUrl('example.com:443');
$validator->setUrl('https://de.wikipedia.org');

$validator->validateAll(); // Validates all Urls in a rush

echo $validator->getScheme('https://de.wikipedia.org'); // Returns https
echo $validator->getScheme('example.com:443'); // Returns https
echo $validator->getScheme('www.example.com'); // Returns http

echo $validator->getUrl('https://de.wikipedia.org'); // Returns https://de.wikipedia.org
echo $validator->getUrl('example.com:443'); // Returns https://example.com
echo $validator->getUrl('www.example.com'); // Returns http://www.example.com

var_dump($validator->getUrls()); 
/* Returns an array : 
 [
    'https://de.wikipedia.org' => [
        'scheme' => 'https', 
        'url' => 'https://de.wikipedia.org'
    ],
    'example.com:443' => [
        'scheme' => 'https', 
        'url' => 'https://example.com:443'
    ],
    'www.example.com' => [
        'scheme' => 'http', 
        'url' => 'http://www.example.com'
    ],
]
 */


$validator = new \basteyy\UrlSchemeValidator\UrlSchemeValidator('//www.example.com');

// Change the Default Scheme
$validator->setDefaultScheme('file');
echo $validator->getUrl(); // Returns file://www.example.com

// Validate direct for web (only http and https)
var_dump($validator->isWebScheme()); // Returns false in this case (case now its file)
var_dump($validator->isWebScheme('https://example.com')); // Returns true