Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if($_GET['Action'] == 'Encode'){
- $array = str_split($_GET['Text']);
- $result = '';
- foreach($array as $item){
- $bin = decbin(ord($item));
- $bin = str_pad($bin, 8, "0", STR_PAD_LEFT);
- $result .= $bin;
- }
- echo $result;
- } elseif($_GET['Action'] == 'Decode'){
- $array = str_replace(' ','',$_GET['Text']);
- if(strlen($array) % 8 !== 0){
- header('Location: Binary.php?Error=1');
- exit;
- } else {
- if(substr_count($_GET['Text'],' ') !== 0){
- header('Location: /Binary.php?Text='.urlencode(str_replace(' ','',$_GET['Text'])).'&Action=Decode');
- exit;
- }
- $array = str_split($array,8);
- $bin = '';
- foreach($array as $item){
- $result .= chr(bindec($item));
- }
- echo $result;
- }
- } elseif(isset($_GET['Text']) && strlen($_GET['Text']) !== 0) {
- $value = str_replace(' ','',$_GET['Text']);
- if(is_numeric($value) && substr_count($value,1)+substr_count($value,0) === strlen($value) && strlen($value) %8 === 0){
- header('Location: /Binary.php?Text='.urlencode($value).'&Action=Decode');
- exit;
- } else {
- header('Location: /Binary.php?Text='.urlencode($_GET['Text']).'&Action=Encode');
- exit;
- }
- } else {
- ?>
- <form action="" method="get">
- <input type="text" name="Text"><br>
- <input type="radio" name="Action" value="Encode">Encode<br>
- <input type="radio" name="Action" value="Decode">Decode<br>
- <input type="submit" value="Submit">
- </form>
- <?php
- }
Advertisement
Add Comment
Please, Sign In to add comment