dtdfl62844 2011-04-09 10:25
浏览 27
已采纳

preg_replace和file_get_contents - 如何获得$ {1}?

I have this piece of code:

echo preg_replace('/\!(.*)\!/', file_get_contents('${1}'), $str);

What it is meant to do is replacing all !...! with the contents of the file specified between the exclamation marks. However, it is not working because ${1} is not getting replaced:

Warning: file_get_contents(${1}) [function.file-get-contents]: failed to open stream: No such file or directory

If I code:

echo preg_replace('/\!(.*)\!/', '${1}', $te);

everything is fine (i.e. the text between !...! is replaced by the text itself).

How can I make the ${1} in file_get_contents also be replaced?

  • 写回答

2条回答 默认 最新

  • doucepei5298 2011-04-09 10:29
    关注
    echo preg_replace_callback('/\!(.*)\!/', function($matches) {
        return file_get_contents($matches[1]);
    }, $str);
    

    There you go. Use preg_replace_callback for this kind of replacements, where you need to call a custom function on the matches that would give the replacement string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题