0

This was difficult to put into a title.

Essentially what I want to do is, when I get some random binary code i need certain number patterns be converted into a declared symbol.

For example :

input: 11100011111 Output: ABF

So essentially

  • 111 = A, 000 = B, 11111 = C, 1111 = D

Note!That the output has to correspond in order so if it started with 111 the output hast to start with A(declared symbol for said pattern). There's a D there also essentially the first four characters of C is the same as D but it must output the one most similar.

First i tried to put this into a for loop so, check string characters, if there's a 1 echo one and vice versa

for ($i=0; $i < $getInput[$i] ; $i++) { 
    if ($getInput[$i] == 1) {
        echo "ONE";
    } elseif ($getInput[$i] == 0) {
        echo "ZERO";
    }   
}

this was just for testing, essentially in this theory i can check if if there is a consecutive pattern so 000 it will refer to an array with different patterns already assigned and then output the symbol whatever was assigned to that pattern.

I also tried using foreaches but due to inexperience...

I did some research and found this topic: How do I check if a string contains a specific word?

which kinda helped since with this code

$a = '1110001111';

if (strpos($a, '1111') !== false) {
    echo 'D';
}

It outputs the character in right order, problem with this approach is that well it kinda means id need to write out if statements repetitively, and secondly if there were to be 1111 in the input, it would take the part 111 and 1111 as two different strings and output both of them

EDIT:

Essentially what i just thought is, i actually need to compare 4 digits to different patterns, i can put the string into an array split it into 4's and then compare with another array which holds the declared patterns (1111 = a etc..), so in a 4 digit binary 0000 there can be about 17 different patterns therefor no need for anything above >4

Then again if there were an odd number of 1's and 0's there'd be some left off. Then these should just be outputted as is. so if there were 1010110 = A110 since A is declared as a pattern of 1010

Dice
  • 49
  • 8
  • 1
    If you have nine ones is that `CD` or `DC`? – Andreas Nov 01 '17 at 20:47
  • @Andreas Good question but i changed the idea a bit. – Dice Nov 01 '17 at 20:57
  • whats the source if this "binary" ? –  Nov 01 '17 at 20:58
  • @nogad what do you mean? exactly? – Dice Nov 01 '17 at 21:01
  • So it's only four-split now? I'll edit my answer. – Andreas Nov 01 '17 at 21:04
  • `Essentially what I want to do is, when I get some random binary code` ... whats the source of this "random binary code"? –  Nov 01 '17 at 21:05
  • @nogad well any really, essentially what i am trying to achieve here is to compress a line of binary code to as small amount of characters as possible. Why? Well I got bored and this is really interesting. essentially if i get the understanding I could go on to zip compressors and such – Dice Nov 01 '17 at 21:13
  • 1
    So what you are saying is that you are trying to reinvent the wheel? There are good explanations both in text and video format on internet on how real compression works. Computerphile has done at least one that is a good watch. – Andreas Nov 01 '17 at 21:19
  • @Andreas ofcourse! But by looking into this in a less conventional way, by trying to Make it myself i learn php and more about compression, thank you for the recommendation, I've taken this as more of a php exercise – Dice Nov 01 '17 at 21:27
  • not exactly a real world php exercise it? you would never actually do this in php. –  Nov 01 '17 at 21:36
  • Fair enough. I'm just saying if it's text compression your doing it wrong :-) think about it: If you want to compress a letter that is in ASCII 8bit you will need to replace it with something with less than 8bit. Although 0 and 1 may seem smaller it's still 8bit (or more if it's UTF or Unicode saved). Look at the video about the big tree and text compression on computerphile. – Andreas Nov 01 '17 at 21:37
  • @nogad the more functions and syntaxes i learn widen my vocabulary in terms of php, i could have not put to words such a definition like a preg_match_all has, i'd be making a mile of if statements essentially – Dice Nov 01 '17 at 21:58
  • @Andreas Nyea It was half way through the start when i thought about this, i will definitely look more into this! – Dice Nov 01 '17 at 21:58

2 Answers2

0

This code will give you an array of the 4 digits and then you can parse that via a foreach and lookup.

<?php
$string = "11111001011100001111";
$matches = [];
preg_match_all("/[1|0]{4}/", $string, $matches);
echo "<pre>";
print_r($matches);

Which the output of that would be:

<pre>Array
(
[0] => Array
    (
        [0] => 1111
        [1] => 1001
        [2] => 0111
        [3] => 0000
        [4] => 1111
    )

)
Thomas
  • 147
  • 1
  • 4
0

You can use str_split to split the string in chunkes of four digits.
Then I use an associative array with replacements.

I loop the match array and echo replacement character.

$str = "111000111111";
//preg_match_all('/(.)\1*/', $str, $match);
$digits = str_split($str, 4);
$replace = array("1110" => "A", "0011" => "B", "1111" => "C");

Foreach($digits as $val){
     Echo $replace[$val];
}

https://3v4l.org/QfQQ8

Andreas
  • 23,610
  • 6
  • 30
  • 62