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!

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

报告相同问题?

悬赏问题

  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人