title: How to get the Entire Function from a file
body: <p>Ok, I'm reading through a file now, line by line.  I know each functions name in the file, since it is defined elsewhere in an XML document.  That is what should be this:</p>
  
  <pre><code>function function_name
  </code></pre>
  
  <p>Where function_name is the name of the function.</p>
  
  <p>I get all of the function definitions from an XML document that I already have put into an array of function names, and I need to grab just those functions from the php file.  And rebuild that php file so that it only has those functions in it.  That is to say, if a php file has more functions than what is defined in the XML  tag, than I need to strip out those functions, and rewrite the .php file with only the functions that the user specified in the XML file.</p>
  
  <p>So, the dilemma I face is how to determine the END of a function reading line by line, and I'm aware that functions can have functions within them.  So I don't want to remove the functions within them.  Just functions that are standalone and aren't defined within the accompanying XML file.  Any ideas on how to do this??</p>
  
  <p>Ok, I'm using the following function now:</p>
  
  <pre><code>//!!! - Used to grab the contents of all functions within a file with the functions array.
  function get_functions($source, $functions = array()) 
  {
      global $txt;
  
      if (!file_exists($source) || !is_readable($source))
          return '';
  
      $tokens = token_get_all(file_get_contents($source));
  
      foreach($functions as $funcName)
      {
          for($i=0,$z=count($tokens); $i&lt;$z; $i++)
          {
              if (is_array($tokens[$i]) &amp;&amp; $tokens[$i][0] == T_FUNCTION &amp;&amp; is_array($tokens[$i+1]) &amp;&amp; $tokens[$i+1][0] == T_WHITESPACE &amp;&amp; is_array($tokens[$i+2]) &amp;&amp; $tokens[$i+2][1] == $funcName)
                  break;
  
              $accumulator = array();
              // collect tokens from function head through opening brace
              while($tokens[$i] != '{' &amp;&amp; ($i &lt; $z)) { 
                 $accumulator[] = is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
                 $i++;
              }
              if($i == $z) {
                  // handle error
                  fatal_error($txt['error_occurred'], false);
              } else {
                 // note, accumulate, and position index past brace
                 $braceDepth = 1; 
                 $accumulator[] = '{';
                 $i++;
              }
              while($braceDepth &gt; 0 &amp;&amp; ($i &lt; $z)) {
                 if(is_array($tokens[$i]))
                    $accumulator[] = $tokens[$i][1];
                 else {
                    $accumulator[] = $tokens[i];
                    if($tokens[$i] == '{') $braceDepth++;
                    else if($tokens[i] == '}') $braceDepth--;
                 }
                 $i++;
              }
              $functionSrc = implode(null,$accumulator);
          }
      }
  
      return $functionSrc;
  }
  </code></pre>
  
  <p>OK, so it takes this php files content:</p>
  
  <pre><code>&lt;?php
  function module_testing($params)
  {
      // Is it installed?
      $test_param = !isset($params['test_param']) ? 'Testing Testing 1 2 3!' : $params['test_param'];
  
      // Grab the params, if they exist.
      if (is_array($params))
      {           
          echo $test_param;
      }
      // Throw an error.
      else
          module_error();
  }
  
  ?&gt;
  </code></pre>
  
  <p>and changes it like so:</p>
  
  <pre><code>&lt;?php
  
  function module_testing($params)
  
  {
  
      // Is it installed?
  
      $test_param  isset$params'test_param'  'Testing Testing 1 2 3!'  $params'test_param'
  
  
  
      // Grab the params, if they exist.
  
      if is_array$params
  
  
  
          echo $test_param
  
  
  
      // Throw an error.
  
      else
  
          module_error
  
  
  
  
  
  ?&gt;
  </code></pre>
  
  <p>As you can see it took a whole bunch of stuff outta here.  And the last closing bracket is missing...  All I need to do is check if the function exists in here <code>function module_testing</code>, and grab the entire function and write it to the same file.  Seems simple enough, but WoW, this is some major coding for just this minor thing IMO...</p>
  
  <blockquote>
    <p>Or I could also check if a function is
    defined in here that isn't within the
    $functions array, if so, than just
    remove that function.  Perhaps it's
    easier with this approach instead??</p>
  </blockquote>