Encode/decode IDs and a token to/from a string

Encode/decode IDs and a token to/from a string.

Functions to encode a set of ID(s) and a token into a string and decode them back into the original values. The string is typically used as part of a tracking key.

An equivalent JS package, @aponica/id-token-codec-js, is available and kept synchronized with this one, so an encoded string created by one package can be decoded by the other.


composer install aponica/id-token-codec-php


Example 1: Encode/decode a single ID w/ token

$zKey = fzEncode( 12345, 'Hello' );
// '3Hello3D7'
$hResult = fhDecode( $zKey );
// [ 'nId' => 12345, 'zToken' => 'Hello' ]
fhDecode(string $zEncoded)
Definition: fhDecode.php:23
fzEncode(int $nId, string $zToken)
Definition: fzEncode.php:34

Example 2: Encode/decode multiple IDs w/ token

$zKey = fzEncodeIds( [ 12, 34, 56 ], 'FOO' );
// '1110FOOCYu'
$hResult = fhDecodeIdsAndToken( $zKey );
// [ 'anIds' => [ 12, 34, 56 ], 'zToken' => 'FOO' ]
fzEncodeIds(array $anIds, string $zToken=null)
Definition: fzEncodeIds.php:31
fhDecodeIdsAndToken(string $zEncoded)
Definition: fhDecodeIdsAndToken.php:28

Example 3: Ignore random token

$zKey = fzEncodeIds( [ 12, 34 ] );
// something like '1109fe129386b5a9c1d5bCY'
$anResult = fanDecodeIds( $zKey );
// [ 12, 34 ]
fanDecodeIds(string $zEncoded)
Definition: fanDecodeIds.php:23

Please Donate!

Help keep a roof over our heads and food on our plates! If you find aponica® open source software useful, please click here to make a one-time or recurring donation via PayPal, credit or debit card. Thank you kindly!


Please contact us if you believe this package is missing important functionality that you'd like to provide.

Under the covers, the code is heavily commented and uses a form of Hungarian notation for data type guidance. If you submit a pull request, please try to maintain the (admittedly unusual) coding style, which is the product of many decades of programming experience.


Copyright 2018-2022 Opplaud LLC and other contributors.


MIT License.


OPPLAUD and aponica are registered trademarks of Opplaud LLC.

Related Links

Official links for this project:

Related projects: