dongzuan4860 2014-03-10 14:34
浏览 26
已采纳

Php include然后抓取当前URL的结尾并将它们组合起来

I am trying to include a file based on the current page of a URL the user is on.

The files are stored in /includes/ in all cases and on the same domain.

If the current URL the user is on is for example:

www.website.com/page/text_i_want.php?info=tree

<?php $url = curPageURL();?><?php include ("{$_SERVER['DOCUMENT_ROOT']}/includes.'/'.$id = substr( $url, strrpos( $url, '/' )+1"  ?>

This is what I have but even if that did work, it would't remove a everything after and including the .php and add .txt to the end

I just want to grab text_i_want from the end of the URL (not including the dynamic part) and add .txt to the end so effectively I am including:

{$_SERVER['DOCUMENT_ROOT']}/includes/text_i_want.txt

I keep getting an ';' error and syntax error, unexpected T_ECHO with what I have.

Update:

<?php include $_SERVER['DOCUMENT_ROOT'] . '/includes/' . basename($_SERVER['REQUEST_URI'], '?'.$_SERVER['QUERY_STRING']); ?>

That code grabs the file name at the end of the URL, which is what I want. I am just unsure how to replace .php with .txt

  • 写回答

2条回答 默认 最新

  • dongre8505 2014-03-10 17:35
    关注

    Solved: (But unsure how efficient or neat this is, but it works)

    I didn't really know what I was doing but somehow I've managed to mix together @SamOwl's code and one I found and edit them slightly to achieve the result In wanted:

    <?php include $_SERVER['DOCUMENT_ROOT'] . '/includes/' . basename($_SERVER['REQUEST_URI'], 'php?'.$_SERVER['QUERY_STRING']) . 'txt'; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据