收获的正则表达式包括和require指令

Sau*_*aul 1 php java regex file

我试图使用正则表达式(在Java中)从PHP文件中获取所有包含指令.

该表达式应仅包含具有表示为非复合字符串文字的文件名的表达式.具有常数或变量的那些不是必需的.

检测应该适用于单引号和双引号,include-s和require-s,以及附加的技巧,_once以及最后但并非最不重要的关键字和函数式调用.

一个粗略的输入样本:

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

?>
Run Code Online (Sandbox Code Playgroud)

并输出:

["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"]
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

cwe*_*ske 7

使用token_get_all.这是安全的,不会让你头疼.如果您需要userland代码,还有PEAR的PHP_Parser.

  • 好吧,仅仅因为你*问*正则表达式并不意味着你真的*想要*他们.在你的情况下,你似乎知道自己在做什么,但可能还有其他人开始想用RE来解决这个问题并最终找到一个更好的解决方案; 就像我最喜欢的bash引用:http://bash.org/?741908.但我想你是对的,我们应该完成这个讨论. (3认同)
  • @David - 呃不.手册中没有提到Zend的任何内容. (2认同)

Jan*_*nen 5

/(?:require|include)(?:_once)?[( ]['"](.*)\.php['"]\)?;/

应该适用于您指定的所有情况,并且仅捕获不带扩展名的文件名

测试脚本:

<?php

$text = <<<EOT
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";

EOT;

$re = '/(?:require|include)(?:_once)?[( ][\'"](.*)\.php[\'"]\)?;/';
$result = array();

preg_match_all($re, $text, $result);

var_dump($result);
Run Code Online (Sandbox Code Playgroud)

要获得您想要的文件名,请阅读$results[1]

我可能应该指出,我也偏向 cweiske 的答案,除非您真的只想进行正则表达式练习(或者想使用 grep 进行此操作),否则您应该使用分词器。


rid*_*ner 5

要准确地执行此操作,您确实需要完全解析PHP源代码.这是因为文本序列:require('a.php');可以出现在它根本不是包含的地方 - 例如注释,字符串和HTML标记.例如,以下不是真正的PHP包含,但将与正则表达式匹配:

<?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>
Run Code Online (Sandbox Code Playgroud)

也就是说,如果您对获得一些误报感到满意,那么下面的单一正则表达式解决方案可以很好地从所有PHP包含的变体中抓取所有文件名:

// 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;
}
Run Code Online (Sandbox Code Playgroud)

附加2011-07-24事实证明OP需要Java而不是PHP 的解决方案.这是一个经过测试的Java程序,几乎完全相同.请注意,我不是Java专家,也不知道如何动态调整数组大小.因此,下面的解决方案(粗略地)设置固定大小的数组(100)以保存文件名数组.

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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @Saul:你真的应该在你的问题中清楚地指出你正在使用的正则表达式引擎的语言.我花了很多时间整理和改进这个(无用的)PHP解决方案.可以使用Java解决问题,但需要重新考虑正则表达式.我现在正在研究这个...... (2认同)