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

Posted on 2007-10-12
Last Modified: 2008-01-09
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.
Question by:DReade83
    LVL 48

    Accepted Solution

    nope not the valid way. This should work:


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


    Author Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
    The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now