Skip to content

Instantly share code, notes, and snippets.

@flashwave
Last active March 9, 2018 21:07
Show Gist options
  • Save flashwave/ca5562fc9355de1e98c6 to your computer and use it in GitHub Desktop.
Save flashwave/ca5562fc9355de1e98c6 to your computer and use it in GitHub Desktop.
an attempt at doing... something? back in 2015
<?php
/*
* Phone Numberpad Encoding
* Provided to you by Flashwave
*
* Pretty much what this does is encode (and decode) strings into other strings (wow technology)
* that consist out of 1-9, # and *.
* This is not really supposed to be anything serious, just something I'm doing out of boredom.
*
* The goal of this is to be able to encode anything and still be able to decode it properly.
*
* Version 1 reference
* [1: ] [2: ABC] [3: DEF]
* [4: GHI] [5: JKL] [6: MNO]
* [7: PQRS] [8: TUV] [9: WXYZ]
* [*: -] [0: ] [#: +]
*
* Version 1 examples
* 2* 2 2# = A B C
* # 2 2# 2## = A B C
* 1 =
* 1# 2 3 4 = 1 2 3
* 2 3 4 = B E H
* 7 =
* 7# = R
* 7## 4 = S H
* ## 7 4 = S J
*/
namespace Flashwave;
class NumberEncode {
// Array with types
protected static $nums = array(
'1' => '',
'2*' => 'A',
'2' => 'B',
'2#' => 'C',
'3*' => 'D',
'3' => 'E',
'3#' => 'F',
'4*' => 'G',
'4' => 'H',
'4#' => 'I',
'5*' => 'J',
'5' => 'K',
'5#' => 'L',
'6*' => 'M',
'6' => 'N',
'6#' => 'O',
'7**' => 'P',
'7*' => 'Q',
'7' => '',
'7#' => 'R',
'7##' => 'S',
'8*' => 'T',
'8' => 'U',
'8#' => 'V',
'9**' => 'W',
'9*' => 'X',
'9' => '',
'9#' => 'Y',
'9##' => 'Z',
'0' => ' '
);
protected static $special = array(
'1**' => '.',
'1*' => ',',
'1#' => '@',
'2*' => '€',
'2' => '$',
'2#' => '£',
'3*' => '%',
'3' => '^',
'3#' => '&',
'4*' => '*',
'4' => '(',
'4#' => ')',
'5*' => '[',
'5' => ']',
'5#' => '{',
'6*' => '}',
'6' => '-',
'6#' => '\\',
'7**' => '!',
'7*' => ';',
'7#' => '\'',
'7##' => '"',
'8*' => '?',
'8' => '/',
'8#' => '<',
'9**' => '>',
'9*' => '¥',
'9#' => '_',
'9##' => '~',
'0*' => '=',
'0' => '|',
'0#' => ':'
);
// Encoder
public static function encode($string) {
// Split each character up into an array
$array = str_split($string);
// Reset the string variable
$string = '';
// Iterate over the characters and generate the new string
foreach($array as $char) {
if(is_numeric($char)) // Append a 1 for numeric characters and don't do anything special
$string .= '1'. $char;
elseif(in_array($char, self::$special)) // Append a 9 for special characters and use the special operator on getChar
$string .= '9'. self::getChar($char, true, false, true);
elseif(ctype_upper($char)) // Append a 7 for uppercase characters and parse like normal
$string .= '7'. self::getChar($char, true);
else // Just parse and don't do anything else
$string .= self::getChar($char, true);
}
// Return the string
return $string;
}
// Decoder
public static function decode($string) {
// Split each character up into an array
$array = str_split($string);
// Reset the string variable
$string = '';
// Complicated foreach action, preg_match_all was a bitch
// But in the end this was a better solution anyway
foreach($array as $key => $char) {
// Skip if the char is a modifier
if(
// Filter standalone #
$char == '#' ||
// Filter standalone *
$char == '*' ||
// Filter NUM prefix
(
$char == '1' &&
(
isset($array[$key + 1]) &&
is_numeric($array[$key + 1]) &&
$array[$key + 1] != '1'
)
) ||
// Filter CAP prefix
(
$char == '7' &&
(
isset($array[$key + 1]) &&
is_numeric($array[$key + 1]) &&
$array[$key + 1] != '7'
)
) ||
// Filter SPECIAL prefix
(
$char == '9' &&
(
isset($array[$key + 1]) &&
is_numeric($array[$key + 1]) &&
$array[$key + 1] != '9'
)
)
) continue;
// Get the correct character
$string .= (
(
( // Just output the number if the leading number is a 1
isset($array[$key - 1]) &&
$array[$key - 1] == '1'
)
) ?
$char :
self::getChar(
$char
.(
( // If the next character is a modifier do modifications
array_key_exists($key + 1, $array) &&
(
$array[$key + 1] == '#' ||
$array[$key + 1] == '*'
)
) ?
$array[$key + 1]
.(
( // Do even more if the character after that is a modifier too
array_key_exists($key + 2, $array) &&
(
$array[$key + 2] == '#' ||
$array[$key + 2] == '*'
)
) ?
$array[$key + 2] :
''
) :
''
),
false, // False for decoding
( // Caps
isset($array[$key - 1]) &&
$array[$key - 1] == '7'
),
( // Special
isset($array[$key - 1]) &&
$array[$key - 1] == '9'
)
)
);
}
// Return the string
return $string;
}
// Get the special character
public static function getChar($char, $type = false, $caps = false, $special = false) {
// Get a copy of the nums or special array depending on the $special option
$nums = $special ? self::$special : self::$nums;
// If $caps or $type = true, uppercase the character
if($caps || $type)
$char = strtoupper($char);
// If $type = true file the array
if($type)
$nums = array_flip($nums);
// If the character exists set $char to it
if(array_key_exists($char, $nums))
$char = ($caps ? $nums[$char] : strtolower($nums[$char]));
else // Otherwise just sort of output a 0
$char = $type ? '0' : '';
return $char;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment