Skip to content

josantonius/php-url

Repository files navigation

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 backslash if it does not exist at the end of the route:

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/

- 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 backslash if it does not exist at the end of the route:

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

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

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

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

- Add backslash 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 all previous tests:

$ composer tests

β˜‘ TODO

  • Create tests
  • Improve documentation

Contribute

  1. Check for open issues or open a new issue to start a discussion around a bug or feature.
  2. Fork the repository on GitHub to start making your changes.
  3. Write one or more tests for the new feature or that expose the bug.
  4. Make code changes to implement the feature or fix the bug.
  5. Send a pull request to get your changes merged and published.

This is intended for large and long-lived objects.

Repository

All files in this repository were created and uploaded automatically with Reposgit Creator.

License

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

Copyright

2017 Josantonius, josantonius.com

If you find it useful, let me know πŸ˜‰

You can contact me on Twitter or through my email.

About

PHP library to access URL information

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 2

  •  
  •  

Languages