嗨,我正试图找到一种方法,如果我写{},并在这些简单之间放一个文件名,它将从服务器获取文件,并包括它在哪里{}是。
$search = "[login]this is a test[members]";
$fetching = preg_replace('#'[([a-zA-Z0-9]+'.[a-z])']#', '', $search);
$fetch->getcontents($fetching);
function getcontents($matches) {
//print $matches;
$remove = array("[","]");
$fetchfile = str_replace("[", "", $matches);
$this->matches = str_replace("]", "", $fetchfile);
$url = $this->matches;
$file = file_get_contents("modules/$url.php", FILE_USE_INCLUDE_PATH);
print $file;
}
最简单的是使用正则表达式函数;
function get_contents($matches) {
//return file_get_contents($matches[1]);
return '::[file='. $matches[1] . ']::';
}
$search_string = 'I''d like to replace stuff, ie. {login.php} and {logout.php}';
$replaced = preg_replace_callback('#'{([a-zA-Z0-9]+'.[a-z]+)'}#', 'get_contents', $search_string);
echo $replaced;
注意;Regex没有测试,但可能会开箱即用。检查http://www.php.net/manual/en/book.pcre.php
——编辑,测试的功能,现在工作!