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

  • Home
  • SEARCH
  • 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 774287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:06:26+00:00 2026-05-14T19:06:26+00:00

After thinking about This Question and giving an answer to it I wanted to

  • 0

After thinking about This Question and giving an answer to it I wanted to do more about that to train myself.

So I wrote a function which will calc the length of an given function. Th given php-file has to start at the beginning of the needed function.
Example: If the function is in a big phpfile with lots of functions, like

/* lots of functions */
function f_interesting($arg) {
    /* function */
}
/* lots of other functions */

then $part3 of my function will require to begin like that (after the starting-{ of the interesting function):

    /* function */
}
/* lots of other functions */

Now that’s not the problem, but I would like to know if there are an cleaner or simplier ways to do this. Here’s my function: (I already cleaned a lot of testing-echo-commands)
(The idea behind it is explained here)

function f_analysis ($part3) {
    if(isset($part3)) {
        $char_array = str_split($part3); //get array of chars
        $end_key = false; //length of function
        $depth = 0; //How much of unclosed '{'
        $in_sstr = false; //is next char inside in ''-String?
        $in_dstr = false; //is nect char inside an ""-String?
        $in_sl_comment = false; //inside an //-comment?
        $in_ml_comment = false; //inside an /* */-comment?
        $may_comment = false; //was the last char an '/' which can start a comment?
        $may_ml_comment_end = false; //was the last char an '*' which may end a /**/-comment?
        foreach($char_array as $key=>$char) {
            if($in_sstr) {
                if ($char == "'") {
                    $in_sstr = false;
                }
            }
            else if($in_dstr) {
                if($char == '"') {
                    $in_dstr = false;
                }
            }
            else if($in_sl_comment) {
                if($char == "\n") {
                    $in_sl_comment = false;
                }
            }
            else if($in_ml_comment) {
                if($may_ml_comment_end) {
                    $may_ml_comment_end = false;
                    if($char == '/') {
                        $in_ml_comment = false;
                    }
                }
                if($char == '*') {
                    $may_ml_comment_end = true;
                }
            }
            else if ($may_comment) {
                if($char == '/') {
                    $in_sl_comment = true;
                }
                else if($char == '*') {
                    $in_ml_comment = true;
                }
                $may_comment = false;
            }
            else {
                switch ($char) {
                    case '{':
                        $depth++;
                        break;
                    case '}':
                        $depth--;
                        break;
                    case '/':
                        $may_comment = true;
                        break;
                    case '"':
                        $in_dstr = true;
                        break;
                    case "'":
                        $in_sstr = true;
                        break;
                }
            }

            if($depth < 0) {
                $last_key = $key;
                break;
            }
        }
    } else echo '<br>$part3 of f_analysis not set!';
    return ($last_key===false) ? false : $last_key+1; //will be false or the length of the function
}
  • 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-14T19:06:26+00:00Added an answer on May 14, 2026 at 7:06 pm

    You could probably reduce the number of state variables a little, but truthfully… yes, it will be messy code. I would probably get rid of $may_ml_comment_end and peek ahead for the next character when I encounter an asterisk, for example. You will need to rewrite your foreach loop to a regular for loop be able to do that without creating a bigger mess though.

    PS: I don’t see you handling the escape character yet. Without the above approach, that would introduce another boolean variable.

    Another problem with your current code is that characters immediately following a / don’t get interpreted as they should. However unlikely

    echo 5/'2';  // NB: no space in between
    

    is valid in PHP and would break your parser.

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

Sidebar

Related Questions

Can I specify interfaces when I declare a member? After thinking about this question
After reading a bit more about how Gnutella and other P2P networks function, I
I'm thinking about this question for a time: when does an ARM7(with 3 pipelines)
I am after so advice really. I have been thinking about a client server
After reading this question , I was reminded of when I was taught Java
EDIT Public health warning - this question includes a false assumption about undefined behaviour.
After experiencing numerous problems with a certain media player, it got me thinking is
After being told by at least 10 people on SO that version control was
I have asked this question on twitter as well the #clojure IRC channel, yet
This question is a spin-off from this one . Some history: when I first

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.