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

search for in the

token_get_all> <tidyNode->isText
[edit] Last updated: Mon, 01 Nov 2010

view this page in

CLIX. Tokenizer 関数

導入

tokenizer 関数は、Zend Engine に組み込まれた PHP tokenizer への インターフェイスを提供します。以下の関数により、 字句解析レベルの言語処理を行うことなく、PHP ソースを解析/修正する ツールを作成することが可能となります。

トークンに関する付録も参照ください。

要件

外部ライブラリを必要としません。

インストール手順

PHP 4.3.0 以降、以下の関数はデフォルトで有効となっています。 これ以前のバージョンの場合、 --enable-tokenizer を指定して PHP をコンパイルする必要があります。 --disable-tokenizer を指定すること により、tokenizer サポートを無効とすることができます。

Windows 版の PHP には この拡張モジュールのサポートが組み込まれています。これらの関数を使用 するために拡張モジュールを追加でロードする必要はありません。

注意: tokenizer の組込みサポートは PHP 4.3.0 で利用可能となりました。

定義済み定数

この拡張モジュールを組み込んで PHP をコンパイルするか、あるいは実行時に 動的にモジュールを読み込むと、付録Q に挙げられている トークンが定数として定義されます。

以下に 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: // we've defined this
           
case T_DOC_COMMENT: // and this
                // コメントの場合は何もしない
               
break;

            default:
               
// それ以外の場合 -> "そのまま"出力
               
echo $text;
                break;
        }
    }
}
?>
目次
token_get_all -- 指定したソースを PHP トークンに分割する
token_name -- 指定した PHP トークンのシンボル名を取得する


add a note add a note User Contributed Notes Tokenizer 関数
lists at 5etdemi dot com 23-Jul-2005 01:33
The tokenizer functions are quite powerful. For example, you can retrieve all of the methods in a given class using an algorithm like:

for each token:
if token is T_FUNCTION then start buffer
if buffer is started then add the current string to the buffer
if token is ( stop buffer

And the great thing is that the class methods will have the right case, so it's a good way to get around the limitations with get_class_methods returning lowercase method names. Also since using a similar algorithm you can read the arguments of a function you can implement Reflections-like functionality into PHP4.

Finally you can use it as a simpler method of extracting Javadoc out of a class file to generate documentation. The util/MethodTable.php class in AMFPHP (http://www.amfphp.org) uses the tokenizer functions to create a method table with all of the arguments, a description, return type, etc. and from that method table it can generate ActionScript that matches the PHP, but it could also be fitted to generate JavaScript, documentation files, or basically anything you put your mind to. I can also see that this could be the base for a class -> WSDL file generator.

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