dongxia2030 2013-01-21 20:10
浏览 40
已采纳

使用preg_replace更改index.php进行测试

In my forum urls, there is "index.php" in it everywhere. I want to replace it with "test". In my PHP files, I have this line:

// Makes it easier to refer to things this way.
    $scripturl = $boardurl . '/index.php';

I tried just changing it to:

// Makes it easier to refer to things this way.
    $scripturl = $boardurl . '/test';

However, that returned 404 errors. I was told that I needed to use preg_replace to make that happen. I looked at the PHP Manual, and it says I need a pattern, replacement, and a subject. I'm confused about the subject part.

I tried this, but no prevail:

// Makes it easier to refer to things this way.
    $scripturl = $boardurl . preg_replace('/index.php','/test','?');

Here is an example URL: "domain.com/index.php?node=forum"

I want it to look like: "domain.com/test?node=forum"

  • 写回答

1条回答 默认 最新

  • dragon4808 2013-01-21 20:19
    关注

    You can use str_replace(). Like this:

    $new_url = str_replace('index.php', 'test/', $original_url);
    

    Note that preg_replace() would do the job too but it is more complex (and powerful). str_replace() fits in this case.

    Just for your info, the subject param for str_replace is the orginal string, in your example the url with 'index.php' in it. Your example would look like:

    $pattern = '/index\.php/';
    $replacement = 'test/';
    $subject = 'http://yoursite.com/index.php?foo=bar';
    
    echo preg_replace($pattern, $replacement, $subject);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图