Skip to content

This is ada_url, a fast standard-compliant PHP library for working with URLs based on the Ada URL parser.

Installation

To use this library in your project, you can clone the repository and build the extension manually:

Terminal window
git clone https://github.com/lnear-dev/ada-url
cd ada-url
phpize
./configure
make
make install

Or install a specific version from release:

Terminal window
wget "https://github.com/lnear-dev/ada-url/archive/refs/tags/1.0.0.tar.gz"
tar -xvf 1.0.0.tar.gz
cd ada-url-1.0.0
phpize
./configure
make
make install

Then, add the following line to your php.ini file:

extension=ada_url.so

Enumerations

HostType

Represents the type of host in a URL.

  • DEFAULT = 0
  • IPV4 = 1
  • IPV6 = 2

SchemeType

Represents the scheme type of a URL.

  • HTTP = 0
  • NOT_SPECIAL = 1
  • HTTPS = 2
  • WS = 3
  • FTP = 4
  • WSS = 5
  • FILE = 6

Functions

URL::parse

Parses a URL and returns a URL object or null.

Example:

$url = "https://example.com/path/to/resource";
$parsedUrl = AdaURL\URL::parse($url);
if ($parsedUrl) {
echo "Protocol: " . $parsedUrl->getProtocol() . "\n";
echo "Host: " . $parsedUrl->getHost() . "\n";
}

URL::parseWithBase

Parses a URL with a base URL and returns a URL object or null.

Example:

$url = "/path/to/resource";
$base = "https://example.com";
$parsedUrl = AdaURL\URL::parseWithBase($url, $base);
if ($parsedUrl) {
echo "Full URL: " . $parsedUrl->getHref() . "\n";
}

URL::idnaToUnicode

Converts a domain name to its Unicode representation.

Example:

$input = "xn--fsqu00a.xn--fsqu00a";
$unicodeDomain = AdaURL\URL::idnaToUnicode($input);
echo "Unicode Domain: " . $unicodeDomain . "\n";

Classes

URL

Represents a URL and provides methods for manipulation.

Example:

$url = AdaURL\URL::parse("https://example.com/path/to/resource");
echo "Protocol: " . $url->getProtocol() . "\n";
echo "Hostname: " . $url->getHostname() . "\n";
echo "Path: " . $url->getPathname() . "\n";

URLSearchParams

Provides methods to work with URL search parameters.

Example:

$searchParams = new AdaURL\URLSearchParams();
$searchParams->append("key", "value1");
$searchParams->append("key", "value2");
echo "Keys: " . implode(", ", $searchParams->keys()) . "\n";
echo "Values for 'key': " . implode(", ", $searchParams->getAll("key")) . "\n";

API

namespace AdaURL;
enum HostType : int {
case DEFAULT = 0;
case IPV4 = 1;
case IPV6 = 2;
}
enum SchemeType : int {
case HTTP = 0;
case NOT_SPECIAL = 1;
case HTTPS = 2;
case WS = 3;
case FTP = 4;
case WSS = 5;
case FILE = 6;
}
class URLSearchParams implements \ArrayAccess, \Countable {
public function __construct(string $init = "") {}
public function __toString(): string {}
public final function get(string $key): ?string {}
public final function getAll(string $key): array {}
public final function has(string $key): bool {}
public final function append(string $key, string $value): void {}
public final function set(string $key, string $value): void {}
public final function sort(): void {}
public final function delete(string $key): void {}
public final function keys(): array {}
public final function values(): array {}
public final function entries(): array {}
public function offsetExists(mixed $offset): bool {}
public function offsetGet(mixed $offset): mixed {}
public function offsetSet(mixed $offset, mixed $value): void {}
public function offsetUnset(mixed $offset): void {}
public function count(): int {}
}
class URL {
public function __construct(string $url = "", ?string $base = null) {}
public function __toString(): string {}
public final function hasCredentials(): bool {}
public final function hasEmptyHostname(): bool {}
public final function hasHostname(): bool {}
public final function hasNonEmptyUsername(): bool {}
public final function hasNonEmptyPassword(): bool {}
public final function hasPort(): bool {}
public final function hasPassword(): bool {}
public final function hasHash(): bool {}
public final function hasSearch(): bool {}
public final function getHref(): string {}
public final function getProtocol(): string {}
public final function getUsername(): string {}
public final function getPassword(): string {}
public final function getHost(): string {}
public final function getHostname(): string {}
public final function getPort(): string {}
public final function getPathname(): string {}
public final function getSearch(): string {}
public final function getHash(): string {}
public final function getOrigin(): string {}
public final function getHostType(): HostType {}
public final function getSchemeType(): SchemeType {}
public final function setHref(string $url): bool {}
public final function setProtocol(string $protocol): bool {}
public final function setUsername(string $username): bool {}
public final function setPassword(string $password): bool {}
public final function setHost(string $host): bool {}
public final function setHostname(string $hostname): bool {}
public final function setPort(string $port): bool {}
public final function setPathname(string $pathname): bool {}
public final function setSearch(string $search): void {}
public final function setHash(string $hash): void {}
public final function setSearchParams(URLSearchParams $searchParams): void {}
public final function clearPort(): void {}
public final function clearHash(): void {}
public final function clearSearch(): void {}
public static function parse(string $url, ?string $base = null): ?URL {}
public static function parseWithBase(string $url, string $base): ?URL {}
public static function canParse(string $url): bool {}
public static function canParseWithBase(string $url, string $base): bool {}
public static function idnaToUnicode(string $input): string {}
public static function idnaToAscii(string $input): string {}
}

License

This library is licensed under the MIT License. See LICENSE for details.