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

search for in the

Tokenizer 関数> <定義済み定数
[edit] Last updated: Fri, 25 May 2012

view this page in

以下に tokenizer を用いた簡単な PHP スクリプトの例を示します。この例は、 PHP ファイルを読み込み、ソースから全てのコメントを削除し、コードのみを 出力するものです。

例1 tokenizer によるコメントの削除

<?php
/*
* T_ML_COMMENT は PHP 5 には存在しません。
* 以下の 3 行で、古いバージョンとの互換性を確保するために
* これらを定義しています。
*
* その次の 2 行では、PHP 5 にのみ存在する T_DOC_COMMENT を定義しています。
* T_ML_COMMENT が存在するかどうかで 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)) {
       
// 簡単な1文字毎のトークン
       
echo $token;
   } else {
       
// トークン配列
       
list($id$text) = $token;

       switch (
$id) { 
           case 
T_COMMENT
           case 
T_ML_COMMENT// これと
           
case T_DOC_COMMENT// これを定義しました
               // コメントの場合は何もしない
               
break;

           default:
               
// それ以外の場合 -> "そのまま"出力
               
echo $text;
               break;
       }
   }
}
?>


add a note add a note User Contributed Notes
support at image-host-script dot com 21-Jul-2009 12:15
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