mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-20 10:58:54 +00:00
83 lines
1.8 KiB
XML
83 lines
1.8 KiB
XML
![]() |
<?xml version="1.0" encoding="iso-8859-1"?>
|
||
|
<!-- $Revision: 1.2 $ -->
|
||
|
|
||
|
<appendix xml: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>
|
||
|
<title>Strip comments with the tokenizer</title>
|
||
|
<programlisting role="php">
|
||
|
<![CDATA[
|
||
|
<?php
|
||
|
/*
|
||
|
* 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')) {
|
||
|
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: // we've defined this
|
||
|
case T_DOC_COMMENT: // and this
|
||
|
// no action on comments
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
// anything else -> output "as is"
|
||
|
echo $text;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
?>
|
||
|
]]>
|
||
|
</programlisting>
|
||
|
</example>
|
||
|
</appendix>
|
||
|
|
||
|
|
||
|
<!-- 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:
|
||
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||
|
vim: et tw=78 syn=sgml
|
||
|
vi: ts=1 sw=1
|
||
|
-->
|
||
|
|