CakeFest 2024: The Official CakePHP Conference

Ejemplos

Aquí hay un simple ejemplo de scripts PHP donde se usa el tokenizer para leer en un archivo PHP, quitar todo los comentarios del archivo original y mostrar solamente el código puro.

Ejemplo #1 Quitar comentarios con el tokenizer

<?php

$source
= file_get_contents('example.php');
$tokens = token_get_all($source);

foreach (
$tokens as $token) {
if (
is_string($token)) {
// simple 1-character token
echo $token;
} else {
// token array
list($id, $text) = $token;

switch (
$id) {
case
T_COMMENT:
case
T_DOC_COMMENT:
// ninguna acción en comentarios
break;

default:
// cualquier otra cosa -> salida "tal cual"
echo $text;
break;
}
}
}
?>
add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top