Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PHP library to access URL information

License

NotificationsYou must be signed in to change notification settings

josantonius/php-url

Latest Stable VersionLicenseTotal DownloadsCICodeCovPSR1PSR4PSR12

Translations:Español

PHP library to access URL information.

Provides an improved replacement for the access to the components of a URL offered by PHP'sparse_url andpathinfo functions.

This library does not format the provided URL, it only makes it easier to access the components.For something more advanced you can use something likeleague/uri-components.



Requirements

  • Operating System: Linux | Windows.

  • PHP versions: 8.1 | 8.2.

Installation

The preferred way to install this extension is throughComposer.

To installPHP URL library, simply:

composer require josantonius/url

The previous command will only install the necessary files,if you prefer todownload the entire source code you can use:

composer require josantonius/url --prefer-source

You can alsoclone the complete repository with Git:

git clone https://github.com/josantonius/php-url.git

Available Classes

Url Class

Josantonius\Url\Url

Create a new instance:

/** * If no URL is provided, the URL of the current page will be generated. * * The generated URL will exclude ports 80 and 443 and include the rest. */publicfunction __construct(null|string$url =null);

Gets authority:

/** * The authority, in "[user-info@][host][:port]" format. * * @var string URL authority or empty string. */publicreadonly string$authority;

Gets the base URL:

/** * The base URL, in "[scheme:][//domain][:port]" format. * * @var string Base URL or empty string. */publicreadonly string$base;

Gets the path basename:

/** * The path basename, in "[filename][.extension]" format. * * @var string URL path basename or empty string. */publicreadonly string$basename;

Gets the path dirname:

/** * The path dirname, in "[dirname]" format. * * @var string URL path dirname or empty string. */publicreadonly string$dirname;

Gets the path basename extension:

/** * The path basename extension, in "[extension]" format. * * @var string URL path basename extension or empty string. */publicreadonly string$extension;

Gets the path filename:

/** * The path filename, in "[filename]" format. * * @var string URL path filename or empty string. */publicreadonly string$filename;

Gets fragment:

/** * URL fragment in "[fragment]" format. * * @var string URL fragment or empty string. */publicreadonly string$fragment;

Gets the full URL:

publicreadonly string$full;

Gets hashed fragment:

/** * URL hashed fragment in "[#fragment]" format. * * @var string URL hashed fragment or empty string. */publicreadonly string$hash;

Gets host:

/** * URL host in "[subdomain.][domain][.tld]" format. * * @var string URL host or empty string. */publicreadonly string$host;

Gets path:

/** * URL path in "[path]" format. * * @var string URL path or empty string. */publicreadonly string$path;

Gets the query parameters:

/** * URL query parameters in array format. * * @var array<string, mixed> URL query parameters or empty string. */publicreadonly array$parameters;

Gets password:

/** * URL password in "[password]" format. * * @var string URL password or empty string. */publicreadonly string$password;

Gets port:

/** * URL port in "[port]" format. * * @var string URL port or empty string. */publicreadonly int|string$port;

Gets scheme:

/** * URL scheme in "[scheme]" format. * * @var string URL scheme or empty string. */publicreadonly string$scheme;

Gets path segments:

/** * URL path segments in array format. * * @var string[] URL path segments or empty string. */publicreadonly array$segments;

Gets query:

/** * URL query in "[query]" format. * * @var string URL query or empty string. */publicreadonly string$query;

Gets username:

/** * URL username in "[username]" format. * * @var string URL username or empty string. */publicreadonly string$username;

Usage

Example of use for this library:

Create a new instance using the current URL

useJosantonius\Url\Url;$url =newUrl();

Create a new instance using custom URL

useJosantonius\Url\Url;$url =newUrl('https://domain.com');

Gets authority

useJosantonius\Url\Url;$url =newUrl();// https://domain.com$url->authority;// "domain.com"$url =newUrl('https://user:pass@sub.domain.com:90/en/');$url->authority;// "user:pass@sub.domain.com:90"$url =newUrl('https://user:pass@sub.domain.com/en/');$url->authority;// "user:pass@sub.domain.com"$url =newUrl('https://sub.domain.com/en/');$url->authority;// "sub.domain.com"

Gets base URL

useJosantonius\Url\Url;$url =newUrl();// https://user:pass@domain.com:80/en/$url->base;// "https://domain.com"$url =newUrl('https://domain.com:80/?tag=bug');$url->base;// "https://domain.com:80"$url =newUrl('https://domain.com/en/');$url->base;// "https://domain.com"

Gets the path basename

useJosantonius\Url\Url;$url =newUrl();// https://domain.com/search.php$url->basename;// "search.php"$url =newUrl('https://domain.com/en/web/docs/search.php?tag=bug');$url->basename;// "search.php"$url =newUrl('https://domain.com/en/web/docs?tag=bug');$url->basename;// "docs"

Gets the path dirname

useJosantonius\Url\Url;$url =newUrl();// https://domain.com/search.php$url->dirname;// "/"$url =newUrl('https://domain.com/en/web/docs/search.php?tag=bug');$url->dirname;// "/en/web/docs"$url =newUrl('https://domain.com/en/web/docs?tag=bug');$url->dirname;// "/en/web"

Gets the path basename extension

useJosantonius\Url\Url;$url =newUrl();// https://domain.com/search.php$url->extension;// "php"$url =newUrl('https://domain.com/en/web/docs/search.php?tag=bug');$url->extension;// "php"$url =newUrl('https://domain.com/en/web/docs?tag=bug');$url->extension;// ""

Gets the path filename

useJosantonius\Url\Url;$url =newUrl();// https://domain.com/search.php$url->filename;// "search"$url =newUrl('https://domain.com/en/web/docs/search.php?tag=bug');$url->filename;// "search"$url =newUrl('https://domain.com/docs?tag=bug');$url->filename;// "docs"

Gets fragment

useJosantonius\Url\Url;$url =newUrl();// https://domain.com#top$url->fragment;// "top"$url =newUrl('https://domain.com/en/web/docs#top');$url->fragment;// "top"$url =newUrl('https://domain.com');$url->fragment;// ""

Gets the full URL

useJosantonius\Url\Url;$url =newUrl();// https://domain.com:80$url->full;// "https://domain.com"$url =newUrl('https://user:pass@sub.domain.com:90/en/');$url->full;// "https://user:pass@sub.domain.com:90/en/"

Gets hashed fragment

useJosantonius\Url\Url;$url =newUrl();// https://domain.com#top$url->hash;// "#top"$url =newUrl('https://domain.com/en/web/docs#top');$url->hash;// "#top"$url =newUrl('https://domain.com');$url->hash;// ""

Gets host

useJosantonius\Url\Url;$url =newUrl();// https://sub.domain.com$url->host;// "sub.domain.com"$url =newUrl('https://sub.domain.com/en/web/docs#top');$url->host;// "sub.domain.com"$url =newUrl('https://domain.com');$url->host;// "domain.com"$url =newUrl('https://localhost');$url->host;// "localhost"

Gets path

useJosantonius\Url\Url;$url =newUrl();// https://domain.com/en$url->path;// "/en/web/docs/search.php"$url =newUrl('https://domain.com/en/web/docs/search.php');$url->path;// "/en/web/docs/search.php"$url =newUrl('https://domain.com/en/web/docs/');$url->path;// "/en/web/docs/"$url =newUrl('https://domain.com/en?tag=bug');$url->path;// "/en"$url =newUrl('https://domain.com?tag=bug');$url->path;// ""

Gets the query parameters

useJosantonius\Url\Url;$url =newUrl();// https://domain.com/en?tag=bug&order=asc#top$url->parameters;// ["tag" => "bug", "order" => "asc"]$url =newUrl('https://domain.com/en/web/docs/search.php');$url->parameters;// ""

Gets password

useJosantonius\Url\Url;$url =newUrl();// https://:pass@domain.com$url->password;// "pass"$url =newUrl('https://user:pass@domain.com');$url->password;// "pass"$url =newUrl('https://user@domain.com');$url->password;// ""

Gets port

useJosantonius\Url\Url;$url =newUrl();// https://domain.com:90$url->port;// 90$url =newUrl();// https://domain.com:80$url->port;// ""$url =newUrl();// https://domain.com:443$url->port;// ""$url =newUrl('https://domain.com:80/en/');$url->port;// 80$url =newUrl('https://domain.com:443/en/');$url->port;// 443$url =newUrl('https://domain.com/en/');$url->port;// ""

Gets scheme

useJosantonius\Url\Url;$url =newUrl();// http://domain.com$url->scheme;// "http"$url =newUrl('https://domain.com');$url->scheme;// "https"

Gets path segments

useJosantonius\Url\Url;$url =newUrl();// https://domain.com?tag=bug$url->segments;// []$url =newUrl('https://domain.com/en/web/docs/search.php');$url->segments;// ['en', 'web', 'docs', 'search.php']

Gets query

useJosantonius\Url\Url;$url =newUrl();// https://domain.com?tag=bug$url->query;// "tag=bug"$url =newUrl('https://domain.com?tag=bug&order=asc#top');$url->query;// "tag=bug&order=asc"$url =newUrl('https://domain.com');$url->query;// ""

Gets username

useJosantonius\Url\Url;$url =newUrl();// https://user@domain.com$url->username;// "user"$url =newUrl('https://:pass@domain.com');$url->username;// ""$url =newUrl('https://user:pass@domain.com');$url->username;// "user"$url =newUrl('https://domain.com');$url->username;// ""

Tests

To runtests you just needcomposerand to execute the following:

git clone https://github.com/josantonius/php-url.git
cd php-url
composer install

Run unit tests withPHPUnit:

composer phpunit

Run code standard tests withPHPCS:

composer phpcs

RunPHP Mess Detector tests to detect inconsistencies in code style:

composer phpmd

Run all previous tests:

composer tests

TODO

  • Add new feature
  • Improve tests
  • Improve documentation
  • Improve English translation in the README file
  • Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)

Changelog

Detailed changes for each release are documented in therelease notes.

Contribution

Please make sure to read theContributing Guide, before making a pullrequest, start a discussion or report a issue.

Thanks to allcontributors! ❤️

Sponsor

If this project helps you to reduce your development time,you can sponsor me to support my open source work 😊

License

This repository is licensed under theMIT License.

Copyright © 2017-present,Josantonius

About

PHP library to access URL information

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp