2002-05-02 13:22:42 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
2005-09-04 19:39:32 +00:00
|
|
|
<!-- $Revision: 1.20 $ -->
|
|
|
|
<!-- Purpose: basic.other -->
|
|
|
|
<!-- Membership: core -->
|
|
|
|
|
2002-05-02 13:22:42 +00:00
|
|
|
<reference id="ref.tokenizer">
|
2004-02-24 08:46:39 +00:00
|
|
|
<title>Tokenizer Functions</title>
|
2003-04-25 20:22:16 +00:00
|
|
|
<titleabbrev>Tokenizer</titleabbrev>
|
2002-05-02 13:22:42 +00:00
|
|
|
|
|
|
|
<partintro>
|
2002-07-28 13:26:11 +00:00
|
|
|
<section id="tokenizer.intro">
|
2002-07-28 13:24:27 +00:00
|
|
|
&reftitle.intro;
|
2003-02-06 09:38:26 +00:00
|
|
|
<para>
|
2003-03-02 11:56:58 +00:00
|
|
|
The tokenizer functions provide an interface to the
|
|
|
|
PHP tokenizer embedded in the Zend Engine. Using these
|
2003-12-19 15:50:07 +00:00
|
|
|
functions you may write your own PHP source analyzing
|
2003-03-02 11:56:58 +00:00
|
|
|
or modification tools without having to deal with the
|
|
|
|
language specification at the lexical level.
|
2003-02-06 09:38:26 +00:00
|
|
|
</para>
|
2002-07-28 13:24:27 +00:00
|
|
|
<para>
|
2002-07-28 15:14:18 +00:00
|
|
|
See also the <link linkend="tokens">appendix about tokens</link>.
|
2002-07-28 13:24:27 +00:00
|
|
|
</para>
|
|
|
|
</section>
|
2002-09-26 21:59:44 +00:00
|
|
|
|
2003-03-01 00:11:55 +00:00
|
|
|
<section id="tokenizer.requirements">
|
2003-02-28 23:43:56 +00:00
|
|
|
&reftitle.required;
|
|
|
|
&no.requirement;
|
|
|
|
</section>
|
2002-12-02 12:55:46 +00:00
|
|
|
|
|
|
|
&reference.tokenizer.configure;
|
|
|
|
|
2002-07-28 13:24:27 +00:00
|
|
|
&reference.tokenizer.constants;
|
2002-09-26 21:59:44 +00:00
|
|
|
|
2003-02-06 09:38:26 +00:00
|
|
|
<section id="tokenizer.examples">
|
|
|
|
&reftitle.examples;
|
|
|
|
<para>
|
|
|
|
Here is a simple example PHP scripts using the tokenizer that
|
|
|
|
will read in a PHP file, strip all comments from the source
|
|
|
|
and print the pure code only.
|
|
|
|
</para>
|
|
|
|
<example>
|
2003-02-28 23:43:56 +00:00
|
|
|
<title>Strip comments with the tokenizer</title>
|
2003-02-06 09:38:26 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2004-08-28 10:24:19 +00:00
|
|
|
/*
|
|
|
|
* T_ML_COMMENT does not exist in PHP 5.
|
|
|
|
* The following three lines define it in order to
|
|
|
|
* preserve backwards compatibility.
|
|
|
|
*
|
|
|
|
* The next two lines define the PHP 5 only T_DOC_COMMENT,
|
|
|
|
* which we will mask as T_ML_COMMENT for PHP 4.
|
|
|
|
*/
|
|
|
|
if (!defined('T_ML_COMMENT')) {
|
2004-01-07 23:15:48 +00:00
|
|
|
define('T_ML_COMMENT', T_COMMENT);
|
2004-08-28 10:24:19 +00:00
|
|
|
} else {
|
2004-01-08 01:09:33 +00:00
|
|
|
define('T_DOC_COMMENT', T_ML_COMMENT);
|
2004-08-28 10:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$source = file_get_contents('example.php');
|
|
|
|
$tokens = token_get_all($source);
|
|
|
|
|
|
|
|
foreach ($tokens as $token) {
|
2003-02-06 09:38:26 +00:00
|
|
|
if (is_string($token)) {
|
2004-08-28 10:24:19 +00:00
|
|
|
// simple 1-character token
|
|
|
|
echo $token;
|
2003-02-06 09:38:26 +00:00
|
|
|
} else {
|
2004-08-28 10:24:19 +00:00
|
|
|
// token array
|
|
|
|
list($id, $text) = $token;
|
|
|
|
|
|
|
|
switch ($id) {
|
|
|
|
case T_COMMENT:
|
|
|
|
case T_ML_COMMENT: // we've defined this
|
|
|
|
case T_DOC_COMMENT: // and this
|
|
|
|
// no action on comments
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// anything else -> output "as is"
|
|
|
|
echo $text;
|
|
|
|
break;
|
|
|
|
}
|
2003-02-06 09:38:26 +00:00
|
|
|
}
|
2004-08-28 10:24:19 +00:00
|
|
|
}
|
2003-02-06 09:38:26 +00:00
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
2002-05-02 13:22:42 +00:00
|
|
|
</partintro>
|
2002-09-26 21:59:44 +00:00
|
|
|
|
2002-05-02 13:22:42 +00:00
|
|
|
&reference.tokenizer.functions;
|
|
|
|
|
|
|
|
</reference>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
|
|
|
sgml-omittag:t
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
sgml-parent-document:nil
|
|
|
|
sgml-default-dtd-file:"../../../manual.ced"
|
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:nil
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
-->
|
|
|
|
|