Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Forked from kahagon/repl.php
Created February 12, 2013 18:17
Show Gist options
  • Select an option

  • Save xeoncross/4771988 to your computer and use it in GitHub Desktop.

Select an option

Save xeoncross/4771988 to your computer and use it in GitHub Desktop.
Simple PHP REPL that complies and prints the finished highlighted code when done
#!/usr/bin/env php
<?php
$stdin = fopen('php://stdin', 'r');
$lines = array();
register_shutdown_function(function() use(&$lines) {
$source = join("\n", $lines);
$c_string = ini_get('highligth.string'); // get ini values
$c_comment = ini_get('highlight.comment');
$c_keyword = ini_get('highlight.keyword');
$c_default = ini_get('highlight.default');
$c_html = ini_get('highlight.html');
$source = highlight_string("<?php\n". $source, true);
// 30 = gray, 31 = red, 32 = green, 33 = yellow, 34 = blue, 35 = purple, 36 = cyan, 37 = white
$source = str_replace('</span>', "\033[0m", $source);
$source = str_replace('<span style="color: #DD0000">', "\033[0;32m", $source);
$source = str_replace('<span style="color: '.$c_string.'">', "\033[0;32m", $source);
$source = str_replace('<span style="color: '.$c_comment.'">', "\033[0;37m", $source);
$source = str_replace('<span style="color: '.$c_keyword.'">', "\033[0;34m", $source);
$source = str_replace('<span style="color: '.$c_default.'">', "\033[0;31m", $source);
$source = str_replace('<span style="color: '.$c_html.'">', "\033[0;30m", $source);
$source = str_replace('<code>', '', $source);
$source = str_replace('</code>', '', $source);
$source = str_replace('<br />', "\n", $source);
$source = str_replace('&nbsp;', ' ', $source);
$source = html_entity_decode($source);
print "\n" . $source . "\n";
});
$data = '';
while(true) {
print '> ';
$data .= fgets($stdin);
if (preg_match('/\\\\$/', $data)==1) {
$data = trim($data, "\x00..\x1F \x5C");
} else {
if(eval($data) !== FALSE) {
$lines[] = trim($data);
}
$data = '';
}
}
@kahagon
Copy link
Copy Markdown

kahagon commented Apr 1, 2013

Great.
I like this.
Thank you.

@xeoncross
Copy link
Copy Markdown
Author

To add this to your path you can edit your .bashrc config file on linux.

$ vim ~/.bashrc

Then paste in this line

alias phprepl='php /home/www/phprepl.php'

You can reload your bashrc with source

$ source .bashrc

And run it

$ phprepl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment