Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 761219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:48:42+00:00 2026-05-14T15:48:42+00:00

Ok, I’m reading through a file now, line by line. I know each functions

  • 0

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:

function function_name

Where function_name is the name of the function.

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.

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??

Ok, I’m using the following function now:

//!!! - 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<$z; $i++)
        {
            if (is_array($tokens[$i]) && $tokens[$i][0] == T_FUNCTION && is_array($tokens[$i+1]) && $tokens[$i+1][0] == T_WHITESPACE && is_array($tokens[$i+2]) && $tokens[$i+2][1] == $funcName)
                break;

            $accumulator = array();
            // collect tokens from function head through opening brace
            while($tokens[$i] != '{' && ($i < $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 > 0 && ($i < $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;
}

OK, so it takes this php files content:

<?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();
}

?>

and changes it like so:

<?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





?>

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 function module_testing, 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…

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??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:48:42+00:00Added an answer on May 14, 2026 at 3:48 pm

    Ok, guys, I managed to fix this perfectly fine, and on my own, and here is the perfect solution. I want to thank you all for your help with this. Thanks, you guys have gone far beyond helping me here. But I knew this would be a simple solution without using the tokenizer functions. Perhaps you guys have forgotten that I have the name of each function? In any case, thanks again, but the token functions won’t be needed for this.

    Cheers.

    function remove_undefined_functions($source, $functions = array())
    {
        if (!file_exists($source) || !is_readable($source))
            return '';
    
        $code = '';
        $removeStart = false;
    
        $fp = fopen($source, 'rb');
        while (!feof($fp))
        {
            $output = fgets($fp);
            $funcStart = strpos(strtolower($output), 'function');
    
            if ($funcStart !== false)
            {
                foreach($functions as $funcName)
                {
                    if (strpos($output, $funcName) !== false)
                    {
                        $code .= $output;
                        $removeStart = false;
                        break;
                    }
                    else
                        $removeStart = true;
                }
                continue;
            }
            else
            {
                if (substr($output, 0, 2) == '?>' || !$removeStart)
                    $code .= $output;
            }
        }
        fclose($fp);
    
        // Rewrite the file with the functions that are defined.
        $fo = @fopen($source, 'wb');
    
        // Get rid of the extra lines...
        @fwrite($fo, str_replace("\r\n", "\n", $code));
    
        fclose($fo);
    }
    

    And this will make it so that if there is a function inside of a function, than the user will have to define it, otherwise, the function will not work properly. So this isn’t really a big deal to me, since they can have an unlimited amount of functions, and would better suited that each function is a function to itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.