Bilal Choudhary Bilal Choudhary - 1 month ago 7
HTML Question

How to extract complete html source code and save it in a text file by using php

I am trying to extract the source code from a website and want to save it in a text file on wamp server. So kindly help me.

Answer

It is not very complicated, you can get the content of a file by calling file_get_contents() this includes files on the web. Then you just need to write the content to a file and presto you have extracted the .html source and saved it in a text file by using php.

<?php
    // Open a webpage
    $homepage = file_get_contents('http://www.example.com/');
    // echo the homepage to see the content.
    echo $homepage;

    // Set the filename
    $file = 'hp.txt';
    // Write the contents back to the file
    file_put_contents($file, $homepage);
?>

Hope this is what you need.