File: README.md

Recommend this page to a friend!
  Classes of Josantonius  >  PHP URL Manipulation Library  >  README.md  >  Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP URL Manipulation Library
Manipulate and get different types of URLs
Author: By
Last change:
Date: 2 years ago
Size: 11,675 bytes
 

 

Contents

Class file image Download

PHP Url library

Latest Stable Version Latest Unstable Version License Codacy Badge Total Downloads Travis PSR2 PSR4 CodeCov

Versión en español

Library for urls manipulation.

Requirements

This library is supported by PHP versions 5.6 or higher and is compatible with HHVM versions 3.0 or higher.

Installation

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:

Download Url.php:

$ wget https://raw.githubusercontent.com/Josantonius/PHP-Url/master/src/Url.php

Available Methods

Available methods in this library:

- Get URL from the current page:

Url::getCurrentPage();

# Return (string) ? current URL

- Get base URL of the site:

Url::getBaseUrl();

- Get protocol from current or passed URL:

Url::getProtocol($url);

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $url | URL from which to obtain protocol. | string | No | false |

# Return (string) ? http or https

- Check if it is a secure site (SSL):

Url::isSSL($url);

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $url | URL to check protocol. | string | No | false |

# Return (boolean)

- Get the server name:

Url::getDomain($url);

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $url | URL to get domain. | string | No | false |

# Return (string|false) ? server name or false

- Get URI:

Url::getUri();

# Return (string) ? path/URL

- Remove subdirectories from uri if they exist:

Url::getUriMethods();

# Return (string) ? method1/method2/method3

- Set parameters from the url and return url without them:

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

- Get the server port:

Url::getPort();

# Return (int) ? server port

- Add back slash if it does not exist at the end of the route:

Url::addBackSlash($uri, $position);

This method will remove all slashes that are in the indicated position before adding them. E. g. `addBackSlash('sample///',' both');` will return `/sample/`.

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $uri | URI when add back slash. | string | Yes | | | $position | Place where the back slash is placed: 'top', 'end' or 'both'. | string | No | 'end' |

# Return (string|false) ? path/url/ | /path/url | /path/url/ or false if a correct type is not entered.

- Go to the previous url:

Url::previous();

# Return (void)

- Redirect to chosen url:

Url::redirect($url);

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $url | The URL to redirect. | string | Yes | |

# Return (void)

- Converts plain text urls into HTML links:

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

- This function converts and url segment to an safe one:

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

- Get all url parts based on a / seperator:

Url::segmentUri($uri);

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $uri | URI to segment. | string | No | null |

# Return (string) ? segments

- Get first item segment:

Url::getFirstSegment($segments);

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $segments | Segments. | mixed | Yes | |

# Return (string) ? segment

- Get last item segment:

Url::getLastSegment($segments);

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $segments | Segments. | mixed | Yes | |

# Return (string) ? segment

Quick Start

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;

Usage

Example of use for this library:

- Get URL from the current page:

Url::getCurrentPage();

- Get base URL of the site:

Url::getBaseUrl();

- Get protocol from URL:

Url::getProtocol();

Url::getProtocol('https://josantonius.com/developer/');

- Check if it is a secure site (SSL):

Url::isSSL();

Url::isSSL('https://josantonius.com/developer/');

- Get the server name:

Url::getDomain();

Url::getDomain('https://josantonius.com/developer/');

- Get URI:

Url::getUri();

- Remove subdirectories from URI if they exist:

Url::testGetUriMethods();

- Set parameters from the URL and return URL without them:

Url::setUrlParams();

Url::setUrlParams('https://josantonius.com?param-1=value&param-2=value');

Url::setUrlParams('https://josantonius.com/&param-1=value&param-2=value');

- Get the server port:

Url::getPort();

- Add back slash if it does not exist at the end of the route:

Url::addBackSlash('https://josantonius.com');

Url::addBackSlash('https://josantonius.com', 'end');

- Add back slash if it does not exist at the top of the route:

Url::addBackSlash('josantonius.com', 'top');

- Add back slash if it doesn't exist at the top and end of the route:

Url::addBackSlash('josantonius.com', 'both');

- Go to the previous URL:

Url::previous();

- Redirect to chosen URL:

Url::redirect('https://josantonius.com/');

- Converts plain text URLS into HTML links:

Url::autoLink('https://josantonius.com');

- Converts plain text URLS into HTML links with custom name:

Url::autoLink('https://josantonius.com', 'Josantonius');

- Converts and URL segment to an safe one:

Url::generateSafeSlug('https://josantonius.com');

- Get all URL parts based on a / seperator:

Url::segmentUri('/josantonius/developer/');

- Get first item segment from string:

Url::getFirstSegment('/josantonius/developer/');

- Get first item segment from array:

$segments = ['josantonius', 'developer'];

Url::getLastSegment($segments);

- Get last item segment from string:

Url::getLastSegment('/josantonius/developer/');

- Get last item segment from array:

$segments = ['josantonius', 'developer'];

Url::getFirstSegment($segments);

Tests

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 PHP 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.
  • [ ] Refactor code for disabled code style rules. See phpmd.xml and .php_cs.dist.

Contribute

If you would like to help, please take a look at the list of issues or the To Do checklist.

Pull requests

  • Fork and clone.
  • Run the command `composer install` to install the dependencies. This will also install the dev dependencies.
  • Run the command `composer fix` to excute code standard fixers.
  • Run the tests.
  • Create a branch, commit, push and send me a pull request.

Thank you to all the people who already contributed to this project!

<img alt="peter279k" src="https://avatars2.githubusercontent.com/u/9021747?v=4&s=117" height="117" width="117"> | :---:| peter279k|

Repository

The file structure from this repository was created with PHP-Skeleton.

License

This project is licensed under MIT license. See the LICENSE file for more info.

Copyright

2017 - 2018 Josantonius, josantonius.com

If you find it useful, let me know :wink:

You can contact me on Twitter or through my email.


For more information send a message to info at phpclasses dot org.