According to one my last blog post with an exotic usage of dynamic includes with PHP we will keep on with something still more exotic. The idea is to create clean code. And what is cleaner than the blank?
We will encode our PHP code into spaces and new line characters (\n). The idea is simple. We will translate each character of our script into (n) spaces where (n) is the hexadecimal representation of it’s ASCII code.
function decodeText($encodedText) { $out = array(); foreach (explode("\n", $encodedText) as $line) { $lineOut = array(); foreach (explode("\t", $line) as $item) { $lineOut[] = chr(strlen($item)); } $out[] = implode(null, $lineOut); } return implode("\n", $out); } function encodeText($text) { $out = array(); foreach (explode("\n", $text) as $line) { $lineOut = array(); foreach (str_split($line) as $item) { $lineOut[] = str_repeat(" ", ord($item)); } $out[] = implode("\t", $lineOut); } return implode("\n", $out); }
And now we will create two functions to include our files and parse them with PHP. One function to include from raw input and another to include files.
function includeFromFile($file) { $raw = file_get_contents($file); includeFromRaw($raw); } function includeFromRaw($rawText) { $decodedCode = decodeText($rawText); $tmpfname = tempnam("/tmp", "blank"); $handle = fopen($tmpfname, "w"); fwrite($handle, $decodedCode); fclose($handle); ob_start(function($buffer) use ($tmpfname, $decodedCode) { return str_replace($tmpfname, "<pre>" . htmlentities($decodedCode). "</pre>", $buffer); }); include $tmpfname; ob_end_flush(); unlink($tmpfname); }
Now we can use:
$text = encodeText('<?php echo "Gonzalo";' . "\n" . ' print_r(array(1,2,311)); ?>'); includeFromRaw($text);
Or if we save our code with the name “Blank.clean”
includeFromFile("Blank.clean");
Now if you are brave enough you can write code in our new blankAndSpaces programming language, instead of PHP 🙂
please test your code, set travis and put it on github 😀
why are you doing these things, are they related to something of real life application?
Feel free to enter to my github account. Normally all code is there. Small jokes (working joke indeed:) ) like this post not. TDD is widely used, at least in my last projects and the last two are also connected to travis. This small function has been done without TDD. Coding testa after coding implementation is useless (ok, maybe is more readable, but this small pice of code is enought readable, I think)
Speaking about the real life usage of this post, if the reader is crazy enough to use as-is, good for him ;). I was bored and I wrote this small function. I also had the idea of encode recursively a full library (sf2 for example) and try to use it, but time for experiments this holiday day was over :). Anyway this joke has two things that they can be useful. For example the fake eval trick (with the creation and include one file on the fly) is used in a similar way in projects such as Doctrine2. With years I have learn that “real life application” is something very ambiguous. Real life application of someone is different than another’s one. We all work with similar projects but with different problems and approaches. Because of that we need to adapt the libraries and solutions to our “real life application”