<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/book.tokenizer.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'tokenizer.examples.php',
    1 => 'Examples',
    2 => 'Examples',
  ),
  'up' => 
  array (
    0 => 'book.tokenizer.php',
    1 => 'Tokenizer',
  ),
  'prev' => 
  array (
    0 => 'tokenizer.constants.php',
    1 => 'Predefined Constants',
  ),
  'next' => 
  array (
    0 => 'class.phptoken.php',
    1 => 'PhpToken',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/tokenizer/examples.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="tokenizer.examples" class="appendix">
 <h1 class="title">Examples</h1>

 <p class="para">
  Here is a simple example PHP script using the tokenizer that 
  will read in a PHP file, strip all comments from the source
  and print the pure code only.
 </p>
 <div class="example" id="example-1">
  <p><strong>Example #1 Strip comments with the tokenizer</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$source </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$tokens </span><span style="color: #007700">= </span><span style="color: #0000BB">token_get_all</span><span style="color: #007700">(</span><span style="color: #0000BB">$source</span><span style="color: #007700">);<br /><br />foreach (</span><span style="color: #0000BB">$tokens </span><span style="color: #007700">as </span><span style="color: #0000BB">$token</span><span style="color: #007700">) {<br />   if (</span><span style="color: #0000BB">is_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$token</span><span style="color: #007700">)) {<br />       </span><span style="color: #FF8000">// simple 1-character token<br />       </span><span style="color: #007700">echo </span><span style="color: #0000BB">$token</span><span style="color: #007700">;<br />   } else {<br />       </span><span style="color: #FF8000">// token array<br />       </span><span style="color: #007700">list(</span><span style="color: #0000BB">$id</span><span style="color: #007700">, </span><span style="color: #0000BB">$text</span><span style="color: #007700">) = </span><span style="color: #0000BB">$token</span><span style="color: #007700">;<br /><br />       switch (</span><span style="color: #0000BB">$id</span><span style="color: #007700">) { <br />           case </span><span style="color: #0000BB">T_COMMENT</span><span style="color: #007700">: <br />           case </span><span style="color: #0000BB">T_DOC_COMMENT</span><span style="color: #007700">:<br />               </span><span style="color: #FF8000">// no action on comments<br />               </span><span style="color: #007700">break;<br /><br />           default:<br />               </span><span style="color: #FF8000">// anything else -&gt; output "as is"<br />               </span><span style="color: #007700">echo </span><span style="color: #0000BB">$text</span><span style="color: #007700">;<br />               break;<br />       }<br />   }<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
  </div>

 </div>    
</div>
<?php manual_footer($setup); ?>