Library for urls manipulation.
- Requirements
- Installation
- Available Methods
- Quick Start
- Usage
- Tests
- TODO
- Contribute
- Repository
- License
- Copyright
This library is supported by PHP versions 5.6 or higher and is compatible with HHVM versions 3.0 or higher.
The preferred way to install this extension is through Composer.
To install PHP Url library, simply:
$ composer require Josantonius/Url
The previous command will only install the necessary files, if you prefer to download the entire source code you can use:
$ composer require Josantonius/Url --prefer-source
You can also clone the complete repository with Git:
$ git clone https://github.com/Josantonius/PHP-Url.git
Or install it manually:
$ wget https://raw.githubusercontent.com/Josantonius/PHP-Url/master/src/Url.php
Available methods in this library:
Url::getCurrentPage();
# Return (string) β current URL
Url::getBaseUrl();
Url::getProtocol($url);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$url | URL from which to obtain protocol. | string | No | false |
# Return (string) β http or https
Url::isSSL($url);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$url | URL to check protocol. | string | No | false |
# Return (boolean)
Url::getDomain($url);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$url | URL to get domain. | string | No | false |
# Return (string|false) β server name or false
Url::getUri();
# Return (string) β path/URL
Url::getUriMethods();
# Return (string) β method1/method2/method3
If a url is received as: http://www.web.com/&key=value&key-2=value params will be saved as GET values and return: http://www.web.com/.
If a url is received as: http://www.web.com/?key=value&key-2=value GET parameters are maintained and return: http://www.web.com/.
Url::setUrlParams($url);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$url | URL to get params. | string | No | false |
# Return (string|false) β URL without parameters
Url::getPort();
# Return (int) β server port
Url::addBackslash($uri, $position);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$uri | URI when add backslash. | string | Yes | |
$position | Place where the backslash is placed: 'top', 'end' or 'both'. | string | No | 'end' |
# Return (string) β path/url/ | /path/url | /path/url/
Url::previous();
# Return (void)
Url::redirect($url);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$url | The URL to redirect. | string | Yes |
# Return (void)
Second argument will be used as the url label <a href=''>$custom</a>
.
Url::autoLink($url, $custom);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$url | URL where link. | string | Yes | |
$custom | If provided, this is used for the link label. | string | No | null |
# Return (string) β returns the data with links created around urls
For example: test name @132
will be converted to test-name--123
.
It will also return all letters in lowercase.
Url::generateSafeSlug($slug);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$slug | URL slug to clean up. | string | Yes |
# Return (string) β slug
Url::segmentUri($uri);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$uri | URI to segment. | string | No | null |
# Return (string) β segments
Url::getFirstSegment($segments);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$segments | Segments. | mixed | Yes |
# Return (string) β segment
Url::getLastSegment($segments);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$segments | Segments. | mixed | Yes |
# Return (string) β segment
To use this library with Composer:
require __DIR__ . '/vendor/autoload.php';
use Josantonius\Url\Url;
Or If you installed it manually, use it:
require_once __DIR__ . '/Url.php';
use Josantonius\Url\Url;
Example of use for this library:
Url::getCurrentPage();
Url::getBaseUrl();
Url::getProtocol();
Url::getProtocol('https://josantonius.com/developer/');
Url::isSSL();
Url::isSSL('https://josantonius.com/developer/');
Url::getDomain();
Url::getDomain('https://josantonius.com/developer/');
Url::getUri();
Url::testGetUriMethods();
Url::setUrlParams();
Url::setUrlParams('https://josantonius.com?param-1=value¶m-2=value');
Url::setUrlParams('https://josantonius.com/¶m-1=value¶m-2=value');
Url::getPort();
Url::addBackslash('https://josantonius.com');
Url::addBackslash('https://josantonius.com', 'end');
Url::addBackslash('josantonius.com', 'top');
Url::addBackslash('josantonius.com', 'both');
Url::previous();
Url::redirect('https://josantonius.com/');
Url::autoLink('https://josantonius.com');
Url::autoLink('https://josantonius.com', 'Josantonius');
Url::generateSafeSlug('https://josantonius.com');
Url::segmentUri('/josantonius/developer/');
Url::getFirstSegment('/josantonius/developer/');
$segments = ['josantonius', 'developer'];
Url::getLastSegment($segments);
Url::getLastSegment('/josantonius/developer/');
$segments = ['josantonius', 'developer'];
Url::getFirstSegment($segments);
To run tests you just need Composer and to execute the following:
$ git clone https://github.com/Josantonius/PHP-Url.git
$ cd PHP-Url
$ composer install
Run unit tests with PHPUnit:
$ composer phpunit
Run PSR2 code standard tests with PHPCS:
$ composer phpcs
Run all previous tests:
$ composer tests
- Create tests
- Improve documentation
- Check for open issues or open a new issue to start a discussion around a bug or feature.
- Fork the repository on GitHub to start making your changes.
- Write one or more tests for the new feature or that expose the bug.
- Make code changes to implement the feature or fix the bug.
- Send a pull request to get your changes merged and published.
This is intended for large and long-lived objects.
All files in this repository were created and uploaded automatically with Reposgit Creator.
This project is licensed under MIT license. See the LICENSE file for more info.
2017 Josantonius, josantonius.com
If you find it useful, let me know π