diff --git a/README.md b/README.md index cda4e98..efc8766 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,24 @@ # php-flip -PHP port of flip.js + +PHP port of [flip.js][flipjs] + +## Installation + +To install via `composer`, add the following to your `composer.json`: + +``` +"require": { + "joshtronic/php-flip": "dev-master" +} +``` + +Then run `composer update` + +## Usage + +```php +$flip = new joshtronic\Flip; +echo $flip->flip('text to flip'); +``` + +[flipjs]: https://github.com/jergason/flipjs/blob/master/flip.js diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..381e796 --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "joshtronic/php-flip", + "description": "PHP port of flip.js", + "version": "1.0.0", + "keywords": ["string", "flip", "text"], + "homepage": "https://github.com/joshtronic/php-flip", + "license": "MIT", + "authors": [ + { + "name": "Josh Sherman", + "email": "josh@gravityblvd.com", + "homepage": "http://joshtronic" + } + ], + "require": { + "php": ">=5.3.0" + }, + "autoload": { + "psr-4": {"joshtronic\\": "src/"} + } +} diff --git a/src/flip.php b/src/flip.php new file mode 100644 index 0000000..8dca4a9 --- /dev/null +++ b/src/flip.php @@ -0,0 +1,158 @@ + '¡', + '"' => '„', + '&' => '⅋', + "'" => ',', + '(' => ')', + '.' => '˙', + '3' => 'Ɛ', + '4' => 'ᔭ', + '6' => '9', + '7' => 'Ɫ', + ';' => '؛', + '<' => '>', + '?' => '¿', + 'A' => '∀', + 'B' => '𐐒', + 'C' => 'Ↄ', + 'D' => '◖', + 'E' => 'Ǝ', + 'F' => 'Ⅎ', + 'G' => '⅁', + 'J' => 'ſ', + 'K' => '⋊', + 'L' => '⅂', + 'M' => 'W', + 'N' => 'ᴎ', + 'P' => 'Ԁ', + 'Q' => 'Ό', + 'R' => 'ᴚ', + 'T' => '⊥', + 'U' => '∩', + 'V' => 'ᴧ', + 'Y' => '⅄', + '[' => ']', + '_' => '‾', + 'a' => 'ɐ', + 'b' => 'q', + 'c' => 'ɔ', + 'd' => 'p', + 'e' => 'ǝ', + 'f' => 'ɟ', + 'g' => 'ƃ', + 'h' => 'ɥ', + 'i' => 'ı', + 'j' => 'ɾ', + 'k' => 'ʞ', + 'l' => 'ʃ', + 'm' => 'ɯ', + 'n' => 'u', + 'r' => 'ɹ', + 't' => 'ʇ', + 'v' => 'ʌ', + 'w' => 'ʍ', + 'y' => 'ʎ', + '{' => '}', + '‿' => '⁀', + '⁅' => '⁆', + '∴' => '∵', + '¡' => '!', + '„' => '"', + '⅋' => '&', + ',' => '\'', + ')' => '(', + '˙' => '.', + 'Ɛ' => '3', + 'ᔭ' => '4', + '9' => '6', + 'Ɫ' => '7', + '؛' => ';', + '>' => '<', + '¿' => '?', + '∀' => 'A', + '𐐒' => 'B', + 'Ↄ' => 'C', + '◖' => 'D', + 'Ǝ' => 'E', + 'Ⅎ' => 'F', + '⅁' => 'G', + 'ſ' => 'J', + '⋊' => 'K', + '⅂' => 'L', + 'W' => 'M', + 'ᴎ' => 'N', + 'Ԁ' => 'P', + 'Ό' => 'Q', + 'ᴚ' => 'R', + '⊥' => 'T', + '∩' => 'U', + 'ᴧ' => 'V', + '⅄' => 'Y', + ']' => '[', + '‾' => '_', + 'ɐ' => 'a', + 'q' => 'b', + 'ɔ' => 'c', + 'p' => 'd', + 'ǝ' => 'e', + 'ɟ' => 'f', + 'ƃ' => 'g', + 'ɥ' => 'h', + 'ı' => 'i', + 'ɾ' => 'j', + 'ʞ' => 'k', + 'ʃ' => 'l', + 'ɯ' => 'm', + 'u' => 'n', + 'ɹ' => 'r', + 'ʇ' => 't', + 'ʌ' => 'v', + 'ʍ' => 'w', + 'ʎ' => 'y', + '}' => '{', + '⁀' => '‿', + '⁆' => '⁅', + '∵' => '∴', + ); + + private $flipped_guy = array( + '/', '(', ' ', '.', ' ', '0', ' ', '.', '\\', '\\', ')' + ); + + public function __construct() + { + // Maps inverted characters + foreach ($this->map as $a => $b) { + $this->map[$b] = $a; + } + } + + public function flip($string) + { + if (!is_string($string)) { + return null; + } + + $length = strlen($string) - 1; + $results = array(); + + for ($i = $length; $i >= 0; $i--) { + if ($i >= 6 && substr($string, $i - 7, $i) == '(╯°□°)╯') { + $results = array_merge($results, $this->flipped_guy); + $i -= 7; + } else { + $chr = $string[$i]; + $results[] = isset($this->map[$chr]) ? $this->map[$chr] : $chr; + } + } + + return implode('', $results); + } +} +