[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 635
  • Last Modified:

Using PHP replace to text in a page before as it loads

I have the following files:


index.php reads the following:

    include('page.php'); exit();

page.php contains the following text:

<a href="/index.php?page1">Link</a>

What I want to do is be able to insert some PHP code which checks-for and replaces any code that matches with "/index.php?" and to replace it with "/newindex.php?", but by placing the code in the index.php file, not into each individual page.

I've tried using preg_replace code to do this, by placing it into the index.php file before the include, but the page loads without any changes being made. Next I tried doing something which I doubted working; placing the code in the same line as the include, as follows:

preg_replace("^\/index.php\?$", "/newindex.php?", "/index.php?" (include('page.php')); exit();

This resulted in a blank page (the server's error correction isn't setup properly yet, so no error message was displayed).

Am I going about this the right way?

There is a proper reason behind this; to add the preg_replace code into the index.php file of a forum package aimed at replacing all references to "index.php" with another filename once the page has loaded, but I want to test it this way first to see if it'll work.

Any help on this would be seriously greatly appreciated. Thanks in advance.
1 Solution
nope not the valid way. This should work:


function replaceIndex($text)
    return preg_replace('#/index\.php#', '/newindex.php', $text);

DReade83Author Commented:
Awesome! Worked a treat. Cheers mate!! :-D

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now