PHP Brainfuck Generator
“The Brainfuck language is a programming language noted for its extreme minimalism. Designed to challenge and amuse programmers, it is not suitable for practical use.”… But it sure is fun!
After reading the article “How to Shoot Yourself in the Foot in Any Programming Language“, I was challenged to learn this language due to it’s confusing syntax. Turns out, it’s pretty simple. If you understand arrays and pointers then you’ve got it. I can definitely see how it can be a pain to use though. One misplaced “>” and you’re left searching through a maze of pointers.
Here is a script I put together to convert text into BF code using PHP. I had a bit of fun passing messages between my friends using it. My next attempt will be to create this using BF itself…but after looking at the complexity of basic arithmetic functions like multiplcation and division it may never reach the face of this blog.
text2bf.php
<?php
if(!$text = @$argv[1]) die('Usage: '.$argv[0].' text');
$string = "++++++++++[";
for($i = 0; $i < strlen($text); $i++) {
$value = ord($text[$i]);
$remainder = $value % 10;
$value -= $remainder;
if($value > 10) {
$string .= ">";
for($x = 0; $x < $value / 10; $x++)
$string .= "+";
}
}
for($i = 0; $i < strlen($text); $i++) {
$value = ord($text[$i]);
$remainder = $value % 10;
$value -= $remainder;
if($value > 10) {
$string .= "<";
}
}
$string .= "-]";
for($i = 0; $i < strlen($text); $i++) {
$value = ord($text[$i]);
$remainder = $value % 10;
$value -= $remainder;
$string .= ">";
for($x = 0; $x < $remainder; $x++)
$string .= "+";
$string .= ".";
}
echo $string;
?>
And in true Brainfuck fashion:
<?php if(!$text=@$argv[1])die('Usage: '.$argv[0].' text');$string="++++++++++[";for($i=0;$i<strlen($text);$i++){$value=ord($text[$i]);$remainder=$value%10;$value-=$remainder;if($value>10){$string.=">";for($x=0;$x<$value/10;$x++)$string.="+";}}for($i=0;$i<strlen($text);$i++){$value=ord($text[$i]);$remainder=$value%10;$value-=$remainder;if($value>10){$string.="<";}}$string.="-]";for($i=0;$i<strlen($text);$i++){$value=ord($text[$i]);$remainder=$value%10;$value-=$remainder;$string.=">";for($x=0;$x<$remainder;$x++)$string.="+";$string.=".";}echo $string;?>
Example output of text2bf.php "http://ryonsherman.wordpress.com":
++++++++++[>++++++++++>+++++++++++>+++++++++++> +++++++++++>+++++>++++>++++>+++++++++++> ++++++++++++>+++++++++++>+++++++++++>+++++++++++ >++++++++++>++++++++++>+++++++++++>++++++++++> +++++++++>+++++++++++>++++>+++++++++++>+++++++++++ >+++++++++++>++++++++++>+++++++++++>+++++++++++> ++++++++++>+++++++++++>+++++++++++>++++>+++++++++ >+++++++++++ >++++++++++<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<-] >++++.>++++++.>++++++.>++.>++++++++.>+++++++.> +++++++.>++++.>+.>+.>.>+++++.>++++.>+.>++++.>+++++++++.> +++++++.>.>++++++.>+++++++++.>+.>++++.>.>++.>++++.>+.> +++++.>+++++.>++++++.>+++++++++.>+.>+++++++++.
I’m also currently working on a simple interpreter in PHP. Making the code easier to convert for those who do not want to use a BF compiler.

leave a comment