美好的一天。
在define.php中我们有行:
define('test_word','word for test define');
define('test_word2','word for test define2');
define('test_word3','word for test define3');
define('test_word4','word for test define4');
等
在functions.php中的我们有函数,例如:
function word($word){
echo $word;
}
我们有index.php :
<div>{test word}</div>
<div>{test word}</div>
word
- 任何单词或函数,例如它可以是test_word
或单词('测试函数的单词')等。
例如代码页index.php可以是:
<div>{test test_word}</div>
<div>{test word('word for test function')}</div>
结果我们希望获得内容index.php,其中包含{test
和}
之间的替换值。
例如,如果我们的index.php包含代码:
<div>{test test_word}</div>
<div>{test word('word for test function')}</div>
结果我们应该得到:
<div>word for test define</div>
<div>word for test function</div>
我们可以在下一步获得内容:
文件test.php
<?php
include "define.php";
require_once("functions.php");
$content = file_get_content("index.php");
//BUT what make next?
?>
请告诉我下一步我需要什么以及如何正确替换我的示例中的行?
答案 0 :(得分:0)
我对正则表达式不太满意,所以希望有人会对此有所改进,但这就是我想出来满足你的要求
$content = file_get_content("index.php");
$lines = explode("\n",$content);
foreach($lines as $line)
{
$variableFound = preg_match('/{test (\w+)}/',$line, $variables);
$functionFound = preg_match('/{test (\w+)\(\'([\w ]+)\'\)}/',$line, $function);
if($functionFound == 1)
$function[1]($function[2]);
elseif($variableFound == 1)
echo $variables[1];
}
虽然这里有一个限制,那就是你的index.php文件中每行只能有一个函数调用或一个变量,所以这可能是一个交易破坏者,但你可以继续打破这一行,直到你如果这是一个问题,找不到匹配。无论如何,这应该让你开始。
答案 1 :(得分:0)
我认为这应该有效。我已在必要时在代码的注释中提供了解释。
这是test.php-
的代码<?php
include "define.php";
require_once("functions.php");
$content = file_get_contents("index.php");
/*
Splitting the input, so that each line can be individually processed.
*/
$lines = explode("\r\n",$content);
$prefix = "test";
$regex = "/<div>{".$prefix."[ ]([a-zA-Z0-9_]+(?:[ ]*\((?:.*?)?\))?)}<\/div>/";
foreach($lines as $line){
echo "<div>";
//Check if the input matches the regex pattern.
if(preg_match_all($regex, $line, $matches)){
/*
The constant or the function-name is captured in group 2(index 1).
*/
if(count($matches > 1)){
//
$const_func = $matches[1][0];
//Checking if the constant is defined.
if (defined($const_func)){
echo constant($const_func);
}else{
//Check if the variable matches function syntax.
if(preg_match_all("/([a-zA-Z0-9_]+)[ ]*\((.*?)?\)/", $const_func, $func_matches)){
$func_name = $func_matches[1][0];
$func_args_arr = sanitise_args(explode(",",$func_matches[2][0]));
//check if the function is defined.
if(is_callable($func_name)){
call_user_func_array($func_name, $func_args_arr);
}else{
echo "Function not defined";
}
}else{
echo "Invalid String";
}
}
}else{
echo "No match";
}
}else{
echo "No match";
}
echo "</div>";
}
//A function to clean up the arguments.
function sanitise_args($arr){
return array_map(function($v){return trim($v," '\t\n\r\0\x0B");},$arr);
}
?>
输出 -
-----------file - define.php (I've added an extra constant).
define('test_word','word for test define');
define('test_word2','word for test define2');
define('test_word3','word for test define3');
define('test_word4','word for test define4');
define('random_const', 'this is a random constant');
-----------file - functions.php (I've added an extra function).
function word($word){
print_r($word);
}
function sentence($word1, $word2){
echo "$word1 and $word2 are used in this sentence.";
}
-----------file - index.php
<div>{test test_word}</div>
<div>{test test_word2}</div>
<div>{test random_const}</div>
<div>{test word('word for test function')}</div>
<div>{test sentence('enter','sandman')}</div>
-----------OUTPUT
<div>word for test define</div>
<div>word for test define2</div>
<div>this is a random constant</div>
<div>word for test function</div>
<div>enter and sandman are used in this sentence.</div>
代码基本上是做什么 -