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 583177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:45:54+00:00 2026-05-13T14:45:54+00:00

With Regular Expressions I’m trying to remove all the methods/functions from the following code.

  • 0

With Regular Expressions I’m trying to remove all the methods/functions from the following code. Leaving the “global scope” alone. However, I can’t manage to make it match for all the inner content of a method.

<?php
$mother = new Mother();
class Hello
{
    public function FunctionName($value="username",)
    {

    }
    public function ododeqwdo($value='')
    {
        # code...
    }
    public function ofdoeqdoq($value='')
    {
    if(isset($mother)) {
        echo $lol;
    }
    if(lol(9)) {
       echo 'lol';
    }
    }
}
function user()
{
    if(isset($mother)) {
        echo $lol;
    }
    if(lol(9)) {
       echo 'lol';
    }
}
    $mother->global();
function asodaosdo() {

}

The current Regular Expression I have is: (?:(public|protected|private|static)\s+)?function\s+\w+\(.*?\)\s+{.*?} However, it won’t select a method that has brackets inside, like function user().

If someone could point me in the right direction.

  • 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-13T14:45:55+00:00Added an answer on May 13, 2026 at 2:45 pm

    You can’t do this properly with regex. You need to write a parser that can properly parse comments, string literals and nested brackets.

    Regex cannot cope with these cases:

    class Hello
    {
      function foo()
      {
        echo '} <- that is not the closing bracket!';
        // and this: } bracket isn't the closing bracket either!
        /*
        } and that one isn't as well...
        */
      }
    }
    

    EDIT

    Here’s a little demo of how to use the tokenizer function mentioned by XUE Can:

    $source = <<<BLOCK
    <?php
    
    \$mother = new Mother("this function isNotAFunction(\$x=0) {} foo bar");
    
    class Hello
    {
        \$foo = 666;
    
        public function FunctionName(\$value="username",)
        {
    
        }
        private \$bar;
        private function ododeqwdo(\$value='')
        {
            # code...
        }
        protected function ofdoeqdoq    (\$value='')
        {
            if(isset(\$mother)) {
                echo \$lol . 'function() {';
            }
            if(lol(9)) {
               echo 'lol';
            }
        }
    }
    
    function user()
    {
        if(isset(\$mother)) {
            echo \$lol;
        }
        /* comment inside */
        if(lol(9)) {
           echo 'lol';
        }
    }
    /* comment to preserve function noFunction(){} */
    \$mother->global();
    
    function asodaosdo() {
    
    }
    
    ?>
    BLOCK;
    
    if (!defined('T_ML_COMMENT')) {
       define('T_ML_COMMENT', T_COMMENT);
    } 
    else {
       define('T_DOC_COMMENT', T_ML_COMMENT);
    }
    
    // Tokenize the source
    $tokens = token_get_all($source);
    
    // Some flags and counters
    $tFunction = false;
    $functionBracketBalance = 0;
    $buffer = '';
    
    // Iterate over all tokens
    foreach ($tokens as $token) {
        // Single-character tokens.
        if(is_string($token)) {
            if(!$tFunction) {
                echo $token;
            }
            if($tFunction && $token == '{') {
                // Increase the bracket-counter (not the class-brackets: `$tFunction` must be true!)
                $functionBracketBalance++;
            }
            if($tFunction && $token == '}') {
                // Decrease the bracket-counter (not the class-brackets: `$tFunction` must be true!)
                $functionBracketBalance--;
                if($functionBracketBalance == 0) {
                    // If it's the closing bracket of the function, reset `$tFunction`
                    $tFunction = false;
                }
            }
        } 
        // Tokens consisting of (possibly) more than one character.
        else {
            list($id, $text) = $token;
            switch ($id) {
                case T_PUBLIC:
                case T_PROTECTED:
                case T_PRIVATE: 
                    // Don'timmediately echo 'public', 'protected' or 'private'
                    // before we know if it's part of a variable or method.
                    $buffer = "$text ";
                    break; 
                case T_WHITESPACE:
                    // Only display spaces if we're outside a function.
                    if(!$tFunction) echo $text;
                    break;
                case T_FUNCTION:
                    // If we encounter the keyword 'function', flip the `tFunction` flag to 
                    // true and reset the `buffer` 
                    $tFunction = true;
                    $buffer = '';
                    break;
                default:
                    // Echo all other tokens if we're not in a function and prepend a possible 
                    // 'public', 'protected' or 'private' previously put in the `buffer`.
                    if(!$tFunction) {
                        echo "$buffer$text";
                        $buffer = '';
                    }
           }
       }
    }
    

    which will print:

    <?php
    
    $mother = new Mother("this function isNotAFunction($x=0) {} foo bar");
    
    class Hello
    {
        $foo = 666;
    
    
         private $bar;
    
    
    }
    
    
    /* comment to preserve function noFunction(){} */
    $mother->global();
    
    
    
    ?>
    

    which is the original source, only without functions.

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

Sidebar

Ask A Question

Stats

  • Questions 385k
  • Answers 385k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer sql search by redgate is free http://www.red-gate.com/products/sql_search/index.htm Integrates with SSMS May 14, 2026 at 11:25 pm
  • Editorial Team
    Editorial Team added an answer Thanks for your answers, both + 1. I've solved the… May 14, 2026 at 11:25 pm
  • Editorial Team
    Editorial Team added an answer SQLLite is a good way of storing APP. data. Here… May 14, 2026 at 11:24 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.