Search code examples
phphtmlpaypalecho

How to echo php containing html?


How to echo php containing html?

Instead of using <?php readfile("http://www.website.com/footer.php");?> for every part of the website (footer, header, paypal forms, e.t.c.) I want to merge all parts (like below) into one php file, and then echo specific lines of that file into html.

I have done extensive research on Stackoverflow and other platforms, and tried many implementations. Please, can you assist with my below code, and point me to the right direction?

    <header id=header><div class=logo><img src="/assets/images/name.png" alt="Pablito Greco logo" width=130 height=130 /></div><div class=content><div class=inner><h1>Pablito Greco</h1><p>Inspiring Artist &amp; Entrepreneur</p></div></div><nav><ul><li><a href="/#Xtango" title="Xtango">Xtango</a></li><li><a href="/#Ebook" title="Book">Book</a></li><li><a href="/classes/#c" title="Classes">Classes</a></li><li><a href="/blog/#b" title="Blog">Blog</a></li><li><a href="/#Connect" title="Connect">Connect</a></li></ul></nav></header>

    <article id=thankyou><h3 class=major>Thank you</h3><br><br><p><span class="inside"></span>&nbsp;Pablito Greco will promptly follow up with you. Check our projects:<br><br><a href="/classes/#c" title="Toronto Xtango Classes"><span class=url>Toronto Xtango Classes</span></a><br><a href="/tango-night-secrets/#t" title="Tango Night Secrets"><span class=url>Tango Night Secrets</span></a><br><a href="/blog/#b" title="Toronto Tango Blog"><span class=url>Toronto Tango Blog</span></a><br><a href="/gift/#g" title="Gift Certificates"><span class=url>Gift Certificates</span></a><br><a href="/radios/#r" title="Tango Radios"><span class=url>Tango Radios</span></a><br><a href="/links/#l" title="Tango Links"><span class=url>Tango Links</span></a></p></article>

    <form action="https://www.paypal.com/cgi-bin/webscr" method=post target=_blank class=""><input class=special type=submit value=Buy name=submit title="Buy with PayPal"><div class=special2><input class=special3 type=hidden name=cmd value=_s-xclick><input type=hidden name=hosted_button_id value=XXXXXXXXXXX><input type=hidden name=currency_code value=CAD><select name=os0 title="Click and select"><option value="Select:">Select&nbsp;&nbsp;&nbsp;&#x25BC;</option><option value="6 Xtango Group">6 Xtango Group 119</option><option value="12 Xtango Group">12 Xtango Group (+ ebook) 229</option><option value="3 Xtango Private">3 Xtango Private 299</option><option value="6 Xtango Private">6 Xtango Private (+ ebook) 589</option></select></div></form>

    <div class=kato id=kato><a href= rel=nofollow target=_blank title="Youtube"><span class="url urlfooter" title="Subscribe!">YouTube</span></a>&nbsp;&nbsp;<a href= rel=nofollow target=_blank><span class="url urlfooter" title="Tweet-Tweet!">Twitter</span></a>&nbsp;&nbsp;<a href= target=_blank ><span class="katoradios radio"></span></a>&nbsp;&nbsp;<a href= rel=nofollow target=_blank title="Facebook"><span class="url urlfooter" title="Like!">Facebook</span></a>&nbsp;&nbsp;<a href=/sitemap target=_blank title="Site Map"><span class="url urlfooter">Site Map</span></a></div>

Solution

  • You seem to be looking to scrape html elements using php, and then save the scraped html elements to a new html file. A good way to do this, is to use the DomDocument class and xpath queries to find the element. Then create a new html document and append the found elements to the new document, and save to a file. For example:

    <?
    //create array of element tags to be scraped from target url
    $elements = ["header", "div"];
    //get html from target url
    $html = file_get_contents('http://www.nytimes.com');
    
    //instantiate new instance DOMDocument for target html.
    $dom = new DOMDocument();
    libxml_use_internal_errors(TRUE); //disable libxml errors
    $dom->loadHTML($html);
    libxml_clear_errors();
    //instantiate DOMXPath for the ability to do xpath queries.
    //xpath is powerful for this example using basic query can be used to query by class id etc
    //DOMDocument powerful too. Read documents here: http://php.net/manual/en/class.domdocument.php
    $xpath = new DOMXPath($dom);
    
    //instantiate DOMDocument for new HTML document where founds elements from target will be saved to.
    $new_dom = new DOMDocument();
    $new_dom->formatOutput = true;
    //save basic html element to document
    $new_dom->loadXML("<html></html>");
    $new_dom->saveXML();
    
    //iterate over $elements array
    foreach($elements as $element){
        //query by xpath for the element
        $element_node = $xpath->query("//".$element);
        //iterate over found elements and add to the new html document
        foreach($element_node as $row){
            $node = $new_dom->importNode($row, true);
            $new_dom->documentElement->appendChild($node);
            $new_dom->saveXML();
        }
    }
    //save found elements to new html document
    $new_dom->save('test.html');