Git Product home page Git Product logo

base62's Introduction

Base62

Build Status paypal

Base62 encoder and decorder also for big numbers. Useful to short database numeric ids in URLs.

requirements

  • requires PHP >= 7.1.0 or higher
  • Composer
  • GMP (preferred) or BCMath extensions enabled.

Composer

$ composer require base62/base62

Laravel 5

You just need to follow the composer command listed before and then you have to publish the base62.php config file into the config path of Laravel with the following command:

$ php artisan vendor:publish --tag=base62/base62

Then you can change in config/base62.php the default driver that is 'basic' (the PHP encoder implementation) for another supported by your host. It is recomended to use GMP extension because it is the most fast solution. Allowed encoders and decoders drivers are:

  • basic
  • gmp
  • bcmath

Quick Start and Examples

Methods

encode and decode

Encoders/Decoders drivers

  • basic
  • gmp
  • bcmath

Examples

Encoding and decoding litle numbers.

require 'vendor/autoload.php';

use Base62\Base62;

$base62 = new Base62();		// by default use 'basic' driver. It is the default PHP encoder and decoder
$encodedValue = $base62->encode(200000);	// 'Q1O'
$decodedValue = $base62->decode($encodedValue); // 200000

Encoding and decoding big numbers. An important thing you must take in count: your PHP can be 32 or 64 bit, this means that a representation of an integer can take a maximum or 32 or 64 bit. This is a important limitation when you work with big integers, but for solve this problem we have GMP and BCMath native extensions for PHP.

require 'vendor/autoload.php';

use Base62\Base62;

// unsigned bigint (MySQL) 18446744073709551615
$id = '214748364787898954454';

$base62 = new Base62('gmp');
// print encoded base62 number id
$encodedValue = $base62->encode($id);	// '47rhmv5JHMPe'
$decodedValue = $base62->decode($base62->encode($id)); // '214748364787898954454'

Note that encode method uses strings as argument and not an integer. This is the best option by a simple reason. Imagine that you uses an integer that can not been represented by native PHP 32 or 64 bit interpreter, what would happen? Simple, the integer is truncated and can take negative values or a different positive number.

License

MIT License Copyright © 2014-present, Siro Díaz Palazón

base62's People

Contributors

peter279k avatar sirodiaz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

base62's Issues

Remake all the project for v3

The idea is to remove brick/math in favour of gmp, bcmath and native PHP encoders. Also, the idea for v3 is still allow to encode integers (or strings with valid positive numbers) and decode strings. Maybe for new versions i will work to encode and decode other type of data.

Here is the new tasks to done for complete the V3 version:

  • GMP encoder
  • Bcmath encoder
  • PHP encoder
  • Factory method
  • Tests for all "drivers"
  • Laravel 5 support
  • Add documentation
  • Modify examples

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.