智能PHP压缩代码

时间:2013-08-08 13:07:10

标签: php compression

所以我在“编程谜题和代码高尔夫”中看到了这个聪明的主题: We're not strangers... 。最好的答案是PHP代码打印 Never Gonna Give You Up 的歌词。它只有543个字节。

我试图理解这个PHP代码,但我无法理解这是如何工作的。我认为这是基于语法的压缩,但我不知道如何使用未声明的常量,如

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

所以这是代码。这是如何工作的?

<?=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's 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)/-..");

请参阅it working on Ideone

2 个答案:

答案 0 :(得分:7)

让我们逐个分析str_replace参数。

range('-',T)

range()函数返回一个数组,该数组的元素从第一个参数到第二个参数。字符由ASCII值考虑,因此结果为

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
)

为什么T代替"T"? PHP有一个错误,它使未定义的常量作为与常量名称具有相同内容的字符串进行评估。常量T未定义,因此它与"T"相同,后者为代码高尔夫目的保存了两个字符。 q之后也是如此。如果服务器上有错误报告,则会显示有关未定义常量的警告。

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

这会将字符串拆分为q个字符的数组。同样,这使得代码比使用数组文字更短。结果:

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's 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
)

最后一个参数是目标字符串。

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

如果您将数组作为needle和haystack传递给str_replace(),则可以一次更换一个数组。为简单起见,我们只需将"We; n7trangMs"作为taget字符串,然后从;开始替换。用"7"替换"8s"后的第一步(第二个数组中的相应替换):

"We; n8strangMs"

然后将"8"替换为"o "

"We; no strangMs"
带有";"

"'re"

"We're no strangMs"
带有"M"

"er"

"We're no strangers"

简而言之,它是一种基本的压缩算法,您可以在其中找到在原始文本中重复的字符序列,并用单个字符替换它们。解压缩时,该字符将替换为原始序列。通过迭代运行进度,您可以再次压缩一次压缩的文本("o s" =&gt; "8s" =&gt; "7")。

答案 1 :(得分:1)

试试吧!

假定未定义的常量是字符串。这是启用通知时的样子:

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