Skip to content

Instantly share code, notes, and snippets.

@timoschinkel
Created October 5, 2017 13:46
Show Gist options
  • Save timoschinkel/dadeb0043cc1153a7dfef79e20939308 to your computer and use it in GitHub Desktop.
Save timoschinkel/dadeb0043cc1153a7dfef79e20939308 to your computer and use it in GitHub Desktop.
Visual tokenizer using the PHP_Codesniffer tokenizer
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Tokenize</title>
<style>
span.token:hover {
background: powderblue;
}
pre {
background: #f5f5f5;
border: 1px solid #b5b5b5;
padding: 1em;
}
</style>
<?php
require_once 'vendor/autoload.php';
require_once 'vendor/squizlabs/php_codesniffer/autoload.php';
function tokenize(string $file): string
{
$runner = new \PHP_CodeSniffer\Runner();
$runner->config = new \PHP_CodeSniffer\Config(['this array cannot be empty :s'], false);
$runner->init();
$tokenizer = new \PHP_CodeSniffer\Tokenizers\PHP(file_get_contents($file), null);
$tokens = $tokenizer->getTokens();
return implode('', array_map(function ($token) {
$token_content = htmlspecialchars(is_array($token) ?
$token['content'] :
$token);
return sprintf('<span class="token %1$s" title="%1$s">%2$s</span>',
is_array($token) ? htmlspecialchars($token['type']) : '',
$token_content);
}, $tokens));
}
?>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
<label>
File
<input type="file" name="file" />
</label>
<input type="submit" value="Tokenize" />
</form>
<pre id="result">
<?php echo isset($_FILES['file']['tmp_name']) ? tokenize($_FILES['file']['tmp_name']) : ''; ?>
</pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment