WeaselFox
WeaselFox

Reputation: 7380

How to run a php script after the HTML has been fully rendered

I have a beginner php question:

Suppose I have a simple php file on my server creating some HTML, for example this. How can I run a php script that runs over the fully generated HTML code, after all other php scripts have finished, and modifies it? Lets say for example I want to turn all the <li> tags into <p> tags.

Should this be referenced in the php file itself, or is it better to implement it as an output filter? What is the standard way to call a script after the HTML is ready?

Thanks

--EDIT--

To clarify - I want the script to receive the final value of the HTML the server sends to the client - after all server side script have finished running (other PHP), but before any HTML/Javascript is executed on the server side, and output a modified version of it.

Upvotes: 0

Views: 1151

Answers (2)

WeaselFox
WeaselFox

Reputation: 7380

I found a site explaining how to do this. Im going to leave this around for anyone with similar needs:

http://web.archive.org/web/20101216035343/http://dev-tips.com/featured/output-buffering-for-web-developers-a-beginners-guide

Upvotes: 0

Guillaume
Guillaume

Reputation: 29

You should take a look at output buffering in PHP.

Here is a sample code that allows to replace <li> into <p> after generating HTML code and before sending it to the user:

ob_start();

echo '<html>';
    echo '<body>';
        echo '<ul>';
            echo '<li>List element 1</li>';
            echo '<li>List element 2</li>';
        echo '</ul>';
    echo '</body>';
echo '</html>';

$Html = ob_get_clean();

echo str_replace(array('<li>', '</li>'), array('<p>', '</p>'), $Html);

Upvotes: 2

Related Questions