PHP compression smart code

So, I saw this smart topic on the topic "Programming puzzles and golf code": We are not strangers .... The best answer is the PHP code that prints the texts of Never Gonna Give You Up . This is just 543 bytes.

I tried to understand this PHP code, but I can't figure out how this works. I think this is grammar-based compression, but I don't know how to use undeclared constants like

<?php range('-', T); 

So here is the code. How it works?

 <?=str_replace(range('-',T),split(q," I justCannaLE?2Gotta >u=Msta=.q Ng1Nlet? downNrun<rH=5desMt?N>cryNsayRoodbyeNtE< lie5hurt?q We'T3n each@Jor s8lSg6r hear9<ch: but6;Lo7hyL7BInsideCe both3Cha9Ro: S We3KeRa45we;QplBq1)O)NgiT, nPgiT (GqiT? upq howFJeel: q knowqmeq<= q YHq8sqo qt beenqingq'req aqndqmake? q yHq othMqAqay it q wqDqellq I'mqGqouqIq fqLhq tqerq NPq (OohqeTrQqRSna q gqonqve"),"We; n7trangMsL8loT63Ke rules5s8d8I AJull commit4nt'sChatFKink: of6CHldn'tRetKisJrom< ny@Ruy- /A= if?<sk 42DS'tLE 4?;Lo8bli=L7ee.. O,R1)O,R001)/-.."); 

See he is working on Ideone .

+7
php compression
source share
2 answers

Analyze the str_replace parameters one by one.

 range('-',T) 

The range() function returns an array that has elements spanning from the first parameter to the second parameter. Characters are considered by their ASCII values, so the result

 Array ( [0] => - [1] => . [2] => / [3] => 0 [4] => 1 [5] => 2 [6] => 3 [7] => 4 [8] => 5 [9] => 6 [10] => 7 [11] => 8 [12] => 9 [13] => : [14] => ; [15] => < [16] => = [17] => > [18] => ? [19] => @ [20] => A [21] => B [22] => C [23] => D [24] => E [25] => F [26] => G [27] => H [28] => I [29] => J [30] => K [31] => L [32] => M [33] => N [34] => O [35] => P [36] => Q [37] => R [38] => S [39] => T ) 

Why is T instead of "T" ? PHP has an inaccuracy that allows us to evaluate undefined constants as strings with the same contents as the constant name. The constant T not defined, so it matches the "T" , which stores two characters for encoding purposes. The same goes for q later. If the server reports errors, it will display a warning about the constant undefined.

 split(q,"I justCannaLE?2Gotta >u=Msta=.q..."); 

This splits the string into an array with q characters. Again, this makes shorter code than using an array literal. Result:

 Array ( [0] => I justCannaLE?2Gotta >u=Msta=. [1] => Ng1Nlet? downNrun<rH=5desMt?N>cryNsayRoodbyeNtE< lie5hurt? [2] => We'T3n each@Jor s8lSg6r hear9<ch: but6;Lo7hyL7BInsideCe both3Cha9Ro: S We3KeRa45we;QplB [3] => 1)O)NgiT, nPgiT (G [4] => iT? up [5] => howFJeel: [6] => know [7] => me [8] => <= [9] => YH [10] => 8s [11] => o [12] => t been [13] => ing [14] => 're [15] => a [16] => nd [17] => make? [18] => yH [19] => othM [20] => A [21] => ay it [22] => w [23] => D [24] => ell [25] => I'm [26] => G [27] => ou [28] => I [29] => f [30] => Lh [31] => t [32] => er [33] => NP [34] => (Ooh [35] => eTrQ [36] => RSna [37] => g [38] => on [39] => ve ) 

The final parameter is the target string.

 "We; n7trangMsL8loT63Ke rules5s8d8I AJull commit4nt'sChatFKink: of6CHldn'tRetKisJrom< ny@Ruy- /A= if?<sk 42DS'tLE 4?;Lo8bli=L7ee.. O,R1)O,R001)/-.." 

If you pass arrays to str_replace() as a needle and haystack, the replacement is done one at a time. For simplicity, just take "We; n7trangMs" as the taget string and start the replacement with ; . The first step after replacing "7" with "8s" (corresponding replacement in the second array):

 "We; n8strangMs" 

Then replace "8" with "o "

 "We; no strangMs" 

";" with "'re"

 "We're no strangMs" 

"M" with "er"

 "We're no strangers" 

In short, this is a basic compression algorithm in which you will find sequences of characters that are repeated inside the source text and replace them with a single character. When unpacking, this character is replaced by the original sequence. By doing iterative execution, you can compress compressed text once again ( "os" => "8s" => "7" ).

+7
source share

Give it a try!

Undefined Constants are considered strings. This is similar to that with notifications enabled:

 Notice: Use of undefined constant T - assumed 'T' in D:\www\htdocs\test\index.php on line 1 Notice: Use of undefined constant q - assumed 'q' in D:\www\htdocs\test\index.php on line 1 Deprecated: Function split() is deprecated in D:\www\htdocs\test\index.php on line 12 We're no strangers to love You know the rules and so do I [...] Never gonna say goodbye Never gonna tell a lie and hurt you 
+1
source share

All Articles