duanjinchi1982 2011-07-25 08:20
浏览 56
已采纳

PHP检查{login.php}是否找到包含文件的地方

Hi I am trying to find a way that if I write {} and put a file name in between those simples it will fetch the file from the server and include it where that {} is.

$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;
    }
  • 写回答

1条回答 默认 最新

  • douqianxian7008 2011-07-25 08:39
    关注

    Simplest would be to user regex functions;

    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;
    

    note; regex not tested, but will probably work out of the box. Check http://www.php.net/manual/en/book.pcre.php

    --- edit, tested the function, works now!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?