여기에 올바른 답변이 있습니다. 실제로 PHP 구문을 구문 분석합니다. 진정으로 원하는 것을 할 수는 없습니다. 예를 들어 자동 로더 또는 자동 추출 할 수없는 변수 포함 경로가있을 수 있습니다.
<?php
$required_file = '';
$required_files = array();
$is_required_file = false;
// Parse the PHP source using the Tokenizer library (PHP 4.3.0+)
$tokens = token_get_all($text);
foreach ($tokens as $token) {
// Get the token name to make our code more readable.
$token_name = is_int($token[0]) ? token_name($token[0]) : $token[0];
echo $token[0]. ': ' . htmlentities($token[1]) . '<br>';
// Is this token a require/include keyword?
if (in_array($token_name, array('T_INCLUDE', 'T_INCLUDE_ONCE', 'T_REQUIRE', 'T_REQUIRE_ONCE'))) {
$is_required_file = true;
}
elseif ($is_required_file && $token[0] != ';' && $token[0] != '.' && $token_name != 'T_WHITESPACE') {
if ($token_name == 'T_STRING') {
$token[1] = '{{CONST:' . $token[1] . '}}';
}
$required_file .= $token[1] ? trim($token[1], '\'"') : $token[0];
}
elseif ($is_required_file && $token[0] == ';') {
$is_required_file = false;
$required_files[] = trim($required_file, '()');
$required_file = '';
}
}
$required_files
은 파일의 배열입니다. 일치 require
, require_once
, include
및 include_once
과 일치합니다. 다음과 같은 입력에 대한
는 :
<?php
include APPLICATION_MODULES_ROOT . 'test1.php';
include($some_var . 'test55.php');
include('test2.php');
include ('test3.php');
include_once 'test4.php';
include_once('test5.php');
include_once ('test6.php');
require 'test7.php';
require('test8.php');
require ('test9.php');
//require ('test99.php');
require_once 'test10.php';
require_once('test11.php');
require_once ('test12.php');
이와 끝까지 :
Array
(
[0] => {{CONST:APPLICATION_MODULES_ROOT}}test1.php
[1] => test55.php
[2] => test2.php
[3] => test3.php
[4] => test4.php
[5] => test5.php
[6] => test6.php
[7] => test7.php
[8] => test8.php
[9] => test9.php
[10] => test10.php
[11] => test11.php
[12] => test12.php
)
은 당신이해야 할 모든 응용 프로그램이없는 str_replace 또는 유사한을 사용하여, 사용하는 상수를 교체합니다.
중요 사항 : 변수 포함 이름과 함께 작동하지 않습니다. 예 : include $some_path;
. 당신은 그것들을 파싱 할 수 없다.
시도한 코드와 잘못된 코드를 함께 적어주십시오. 코드를 묻는 질문은 해결 된 문제에 대해 최소한의 이해를 보여 주어야합니다. 시도한 해결책, 실패한 이유 및 예상되는 결과를 포함시킵니다. http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist – showdev
아무 잘못도 없었습니다. 소스 파일을 파싱하고 경로를 추출하는 알고리즘이 필요합니다. –
어떻게 든 [get_included_files] (http://www.php.net/manual/en/function.get-included-files.php)를 사용하십시오. –