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.
Who is Participating?
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.