downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

Funciones Tokenizer> <Constantes predefinidas
[edit] Last updated: Fri, 17 May 2013

view this page in

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
/*
* T_ML_COMMENT no existe en PHP 5.
* Las siguientes tres líneas lo definen para
* preservar compatibilidades anteriores.
*
* Las siguientes dos líneas definen el T_DOC_COMMENT de PHP 5,
* el cual será sobreescrito como T_ML_COMMENT para PHP 4.
*/
if (!defined('T_ML_COMMENT')) {
   
define('T_ML_COMMENT'T_COMMENT);
} else {
   
define('T_DOC_COMMENT'T_ML_COMMENT);
}

$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_ML_COMMENT// hemos definido esto
           
case T_DOC_COMMENT// y esto
               // ninguna acción en comentarios
               
break;

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


add a note add a note User Contributed Notes Ejemplos - [1 notes]
up
-1
support at image-host-script dot com
3 years ago
Code snippet posted above is perfect enough, and I just wanted to put same code in a function that gets argument as code and returns the comments stripped code, so that it is easy for a beginner too, to copy and use this code.

<?
   
if (!defined('T_ML_COMMENT')) {
      
define('T_ML_COMMENT', T_COMMENT);
    } else {
      
define('T_DOC_COMMENT', T_ML_COMMENT);
    }
    function
strip_comments($source) {
       
$tokens = token_get_all($source);
       
$ret = "";
        foreach (
$tokens as $token) {
           if (
is_string($token)) {
             
$ret.= $token;
           } else {
              list(
$id, $text) = $token;

              switch (
$id) {
                 case
T_COMMENT:
                 case
T_ML_COMMENT: // we've defined this
                
case T_DOC_COMMENT: // and this
                   
break;

                 default:
                   
$ret.= $text;
                    break;
              }
           }
        }   
        return
trim(str_replace(array('<?','?>'),array('',''),$ret));
    }
?>

1.Now using this function 'strip_comments' for passing code contained in some variable:

<?
$code
= "
<?php
    /* this is comment */
   // this is also a comment
   # me too, am also comment
   echo "
And I am some code...";
?>"
;

  
$code = strip_comments($code);

   echo
htmlspecialchars($code);
?>

Will result output as
<?
  
echo "And I am some code...";
?>

2.Loading from a php file:

<?
   $code
= file_get_contents("some_code_file.php");
  
$code = strip_comments($code);

   echo
htmlspecialchars($code);
?>

3. Loading a php file, stripping comments and saving it back

<?
   $file
= "some_code_file.php"
  
$code = file_get_contents($file);
  
$code = strip_comments($code);

  
$f = fopen($file,"w");
  
fwrite($f,$code);
  
fclose($f);
?>

regards
Ali Imran

 
show source | credits | stats | sitemap | contact | advertising | mirror sites