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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:20:10+00:00 2026-06-05T10:20:10+00:00

Possible Duplicate: A PHP regex to extract php functions from code files I have

  • 0

Possible Duplicate:
A PHP regex to extract php functions from code files

I have a file with a list of functions formatted something like this:

function foo(){
      ...code { code{}  } code...
}

For this particular file I will always place the word ‘function’ all the way to the left and the ending curly brace all the way to the left. As is customary, the code within the function will always be indented. The code within the function may contain any character including curly braces.

I would like to parse the file with PHP to get an associative array of functions where the names of the functions are the keys. I just started with this, here is a very simple start:

$regex = "/function.*/";
preg_match_all($regex, $str, $result, PREG_PATTERN_ORDER);
$arr = $result[0];

print_r($arr);

This produces the following and stops at each new line:

Array
(
    [0] => function foo(){
    [1] => function bar(){
    [2] => function stop(){
    [3] => function go(){
)

I tried changing the regex to:

$regex = "/function.*\n}$/s";

My thinking was that where there is a new-line character directly follwed by a ending-curly brace, \n}$ would match the end of the function. However, this does not work, it produces an array with one long element that contains everything after function foo()

I have not started to get the function name into the key of the associative array.

  • 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-06-05T10:20:13+00:00Added an answer on June 5, 2026 at 10:20 am

    @John R

    This is the regex solution:

    $regex = '~
      function                 #function keyword
      \s+                      #any number of whitespaces 
      (?P<function_name>.*?)   #function name itself
      \s*                      #optional white spaces
      (?P<parameters>\(.*?\))  #function parameters
      \s*                      #optional white spaces
      (?P<body>\{.*?\})        #body of a function
    ~six';
    
    if (preg_match_all($regex, $input, $matches)) {
      print_r($matches);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: php regex <b> to <b> Inside my admin panel I have a
Possible Duplicate: PHP REGEX: Get domain from URL I am working on a project
Possible Duplicate: PHP - send file to user I have written a script that
Possible Duplicate: php date compare I have a date that I take from the
Possible Duplicate: php scandir --> search for files/directories I have a folder, inside this
Possible Duplicate: php headers already sent error I have attached my code which am
Possible Duplicate: PHP validation/regex for URL Hi, in my form I have a url
Possible Duplicate: PHP EOF shows only one result from loop Hello Seems like I
Possible Duplicate: PHP code to remove everything but numbers i have a loop that
Possible Duplicate: php warning mysql_fetch_assoc I have tried my code and it seems to

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.