Joshua
Joshua

Reputation: 946

How to use PHP to show HTML source

I created a few PHP files for users of a popular hardware site to use to "Metro" their news posts. It works fairly well, you add the title of the article, links etc. and then it spits it out in Metro tile format.

Take a look: http://briandempsey.org.uk/Newstool/index.php

When the user submits, it uses the information provided to create the post. Now, I need to somehow use PHP or some other language to display the code that it generated underneath it so users can just copy and paste it. I'm not sure how to do this.

Upvotes: 1

Views: 108

Answers (2)

Tony M
Tony M

Reputation: 8292

Since you're passing your form data using the method GET, you could instead pass it to a page that creates a url to pull the html from...

index.php will have the form as you've shown above and will post to urlCreator.php.

form.php can be deleted as it is not needed anymore, the magic will happen in the urlCreator.php file.

urlCreator.php (NEW) will have code in it like so:

<?php
    // urlCreator.php will get variables passed to it from index.php

    // Now create the url using the passed variables
    $url = "http://briandempsey.org.uk/Newstool/form.php?title=" . $_GET['title'] . "&color=" . $_GET['color'] . "&Articlecontent=" . $_GET['Articlecontent'] //and so on to build the whole url

    //Now get the pages html
    $html = file_get_contents($url);
?>

Now that you have the html in a variable you can clean it using str_replace or manipulate it however you'd like.

<?php
    // Still in urlCreator.php

    // With the html in a variable you could now edit out the divs and echo the results
    $html = str_replace("<div style=\"background-color: #008299; height: auto; width: 100%; margin-left: 10px;\">", "", $html); //removes the intro div
    $html = str_replace("</div>", "", $html); //removes the closing div

    //Finally echo out the html to the page for copy+paste purposes
    echo $html;
?>

Upvotes: 0

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29965

header('Content-Type: text/plain');

Upvotes: 6

Related Questions