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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:51:02+00:00 2026-05-23T23:51:02+00:00

I am trying to harvest all inclusion directives from a PHP file using a

  • 0

I am trying to harvest all inclusion directives from a PHP file using a regular expression (in Java).

The expression should pick up only those which have file names expressed as unconcatenated string literals. Ones with constants or variables are not necessary.

Detection should work for both single and double quotes, include-s and require-s, plus the additional trickery with _once and last but not least, both keyword- and function-style invocations.

A rough input sample:

<?php

require('a.php');
require 'b.php';
require("c.php");
require "d.php";

include('e.php');
include 'f.php';
include("g.php");
include "h.php";

require_once('i.php');
require_once 'j.php';
require_once("k.php");
require_once "l.php";

include_once('m.php');
include_once 'n.php';
include_once("o.php");
include_once "p.php";

?>

And output:

["a.php","b.php","c.php","d.php","f.php","g.php","h.php","i.php","j.php","k.php","l.php","m.php","n.php","o.php","p.php"]

Any ideas?

  • 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-23T23:51:03+00:00Added an answer on May 23, 2026 at 11:51 pm

    To do this accurately, you really need to fully parse the PHP source code. This is because the text sequence: require('a.php'); can appear in places where it is not really an include at all – such as in comments, strings and HTML markup. For example, the following are NOT real PHP includes, but will be matched by the regex:

    <?php // Examples where a regex solution gets false positives:
        /* PHP multi-line comment with: require('a.php'); */
        // PHP single-line comment with: require('a.php');
        $str = "double quoted string with: require('a.php');";
        $str = 'single quoted string with: require("a.php");';
    ?>
        <p>HTML paragraph with: require('a.php');</p>
    

    That said, if you are happy with getting a few false positives, the following single regex solution will do a pretty good job of scraping all the filenames from all the PHP include variations:

    // Get all filenames from PHP include variations and return in array.
    function getIncludes($text) {
        $count = preg_match_all('/
            # Match PHP include variations with single string literal filename.
            \b              # Anchor to word boundary.
            (?:             # Group for include variation alternatives.
              include       # Either "include"
            | require       # or "require"
            )               # End group of include variation alternatives.
            (?:_once)?      # Either one may be the "once" variation.
            \s*             # Optional whitespace.
            (               # $1: Optional opening parentheses.
              \(            # Literal open parentheses,
              \s*           # followed by optional whitespace.
            )?              # End $1: Optional opening parentheses.
            (?|             # "Branch reset" group of filename alts.
              \'([^\']+)\'  # Either $2{1]: Single quoted filename,
            | "([^"]+)"     # or $2{2]: Double quoted filename.
            )               # End branch reset group of filename alts.
            (?(1)           # If there were opening parentheses,
              \s*           # then allow optional whitespace
              \)            # followed by the closing parentheses.
            )               # End group $1 if conditional.
            \s*             # End statement with optional whitespace
            ;               # followed by semi-colon.
            /ix', $text, $matches);
        if ($count > 0) {
            $filenames = $matches[2];
        } else {
            $filenames = array();
        }
        return $filenames;
    }
    

    Additional 2011-07-24 It turns out the OP wants a solution in Java not PHP. Here is a tested Java program which is nearly identical. Note that I am not a Java expert and don’t know how to dynamically size an array. Thus, the solution below (crudely) sets a fixed size array (100) to hold the array of filenames.

    import java.util.regex.*;
    public class TEST {
        // Set maximum size of array of filenames.
        public static final int MAX_NAMES = 100;
        // Get all filenames from PHP include variations and return in array.
        public static String[] getIncludes(String text)
        {
            int count = 0;                          // Count of filenames.
            String filenames[] = new String[MAX_NAMES];
            String filename;
            Pattern p = Pattern.compile(
                "# Match include variations with single string filename. \n" +
                "\\b             # Anchor to word boundary.              \n" +
                "(?:             # Group include variation alternatives. \n" +
                "  include       # Either 'include',                     \n" +
                "| require       # or 'require'.                         \n" +
                ")               # End group of include variation alts.  \n" +
                "(?:_once)?      # Either one may have '_once' suffix.   \n" +
                "\\s*            # Optional whitespace.                  \n" +
                "(?:             # Group for optional opening paren.     \n" +
                "  \\(           # Literal open parentheses,             \n" +
                "  \\s*          # followed by optional whitespace.      \n" +
                ")?              # Opening parentheses are optional.     \n" +
                "(?:             # Group for filename alternatives.      \n" +
                "  '([^']+)'     # $1: Either a single quoted filename,  \n" +
                "| \"([^\"]+)\"  # or $2: a double quoted filename.      \n" +
                ")               # End group of filename alternativess.  \n" +
                "(?:             # Group for optional closing paren.     \n" +
                "  \\s*          # Optional whitespace,                  \n" +
                "  \\)           # followed by the closing parentheses.  \n" +
                ")?              # Closing parentheses is optional .     \n" +
                "\\s*            # End statement with optional ws,       \n" +
                ";               # followed by a semi-colon.               ",
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.COMMENTS);
            Matcher m = p.matcher(text);
            while (m.find() && count < MAX_NAMES) {
                // The filename is in either $1 or $2
                if (m.group(1) != null) filename = m.group(1);
                else                    filename = m.group(2);
                // Add this filename to array of filenames.
                filenames[count++] = filename;
            }
            return filenames;
        }
        public static void main(String[] args)
        {
            // Test string full of various PHP include statements.
            String text = "<?php\n"+
                "\n"+
                "require('a.php');\n"+
                "require 'b.php';\n"+
                "require(\"c.php\");\n"+
                "require \"d.php\";\n"+
                "\n"+
                "include('e.php');\n"+
                "include 'f.php';\n"+
                "include(\"g.php\");\n"+
                "include \"h.php\";\n"+
                "\n"+
                "require_once('i.php');\n"+
                "require_once 'j.php';\n"+
                "require_once(\"k.php\");\n"+
                "require_once \"l.php\";\n"+
                "\n"+
                "include_once('m.php');\n"+
                "include_once 'n.php';\n"+
                "include_once(\"o.php\");\n"+
                "include_once \"p.php\";\n"+
                "\n"+
                "?>\n";
            String filenames[] = getIncludes(text);
            for (int i = 0; i < MAX_NAMES && filenames[i] != null; i++) {
                System.out.print(filenames[i] +"\n");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to keep all the presentation stuff in the xhtml on this project and
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
Trying to honor a feature request from our customers, I'd like that my application,
Trying to learn ASP MVC coming from Linux/LAMP background (in other words I'm a
Trying to load a file into python. It's a very big file (1.5Gb), but
trying to learn windows programming in java, want to display a image to a
Trying to setup an SSH server on Windows Server 2003. What are some good
Trying to get my css / C# functions to look like this: body {
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to make a make generic select control that I can dynamically add elements

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.