Example #1
0
<?php

require __DIR__ . '/../vendor/autoload.php';
$getValidUrl = function ($url) {
    $parser = new \Riimu\Kit\UrlParser\UriParser();
    $parser->setMode(\Riimu\Kit\UrlParser\UriParser::MODE_UTF8);
    $uri = $parser->parse($url);
    if ($uri === null) {
        return false;
    } elseif (!in_array($uri->getScheme(), ['http', 'https'], true)) {
        return false;
    } elseif ($uri->getTopLevelDomain() === $uri->getHost()) {
        return false;
    }
    return (string) $uri;
};
$normalized = null;
if (isset($_POST['url'])) {
    $normalized = $getValidUrl($_POST['url']);
}
?>
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Validate an URL</title>
 </head>
 <body>
<?php 
if ($normalized === false) {
    printf("  <p>The URL '<code>%s</code>' is <b>not valid</b></p>" . PHP_EOL, htmlspecialchars($_POST['url'], ENT_QUOTES | ENT_HTML5, 'UTF-8'));
Example #2
0
<?php

require __DIR__ . '/../vendor/autoload.php';
$parser = new \Riimu\Kit\UrlParser\UriParser();
$uri = $parser->parse('http://*****:*****@www.example.com:8080/site/index.php?action=login&prev=index#form');
echo $uri->getScheme() . PHP_EOL;
// outputs: http
echo $uri->getUsername() . PHP_EOL;
// outputs: jane
echo $uri->getPassword() . PHP_EOL;
// outputs: pass123
echo $uri->getHost() . PHP_EOL;
// outputs: www.example.com
echo $uri->getTopLevelDomain() . PHP_EOL;
// outputs: com
echo $uri->getPort() . PHP_EOL;
// outputs: 8080
echo $uri->getStandardPort() . PHP_EOL;
// outputs: 80
echo $uri->getPath() . PHP_EOL;
// outputs: /site/index.php
echo $uri->getPathExtension() . PHP_EOL;
// outputs: php
echo $uri->getQuery() . PHP_EOL;
// outputs: action=login&prev=index
echo $uri->getFragment() . PHP_EOL;
// outputs: form
// [0 => 'site', 1 => 'index.php']
echo implode(', ', $uri->getPathSegments()) . PHP_EOL;
// ['action' => 'login', 'prev' => 'index']
foreach ($uri->getQueryParameters() as $name => $value) {