从包含html的php文件中回显特定行

时间:2017-11-18 21:00:58

标签: php html paypal echo

我想要更新,而不是将<?php readfile("http://www.website.com/footer.php");?>用于网站的每个部分(页脚,标题,贝宝形式等),我如何合并所有部分(如下所示)一个php文件,然后将该文件的特定行回显到html。

    <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>

1 个答案:

答案 0 :(得分:0)

您似乎希望使用php抓取html元素,然后将已删除的html元素保存到新的html文件中。执行此操作的一种好方法是使用DomDocument类和xpath查询来查找元素。然后创建一个新的html文档并将找到的元素追加到新文档中,并保存到文件中。例如:

<?
//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');
相关问题