Skip to content

Instantly share code, notes, and snippets.

@kijtra
Last active September 10, 2016 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kijtra/a54fe198df1829ec142cf03c5d1ac63e to your computer and use it in GitHub Desktop.
Save kijtra/a54fe198df1829ec142cf03c5d1ac63e to your computer and use it in GitHub Desktop.
#PHP Numeric to Alphabet OR Alphabet to Numeric function very simple
<?php
function numalpha($value) {
if (ctype_digit((string) $value)) {
return strtr(base_convert($value, 10, 36), '0123456789', 'ABCDEFGHIJ');
} elseif (ctype_alpha($value)) {
return base_convert(strtr($value, 'ABCDEFGHIJ', '0123456789'), 36, 10);
}
return false;
}
/*
- Usage -
$num = '321654987';
$toStr = numalpha($num);
$toNum = numalpha($toStr);
var_dump([
'origin' => $num,
'toStr' => $toStr,
'toNum' => $toNum
]);
- Result -
array(3) {
["origin"]=> string(9) "321654987"
["toStr"]=> string(6) "FbiGkr"
["toNum"]=> string(9) "321654987"
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment