【问题标题】:Echo specific lines from php file containing html从包含 html 的 php 文件中回显特定行
【发布时间】:2018-05-02 10:21:43
【问题描述】:

我想更新网站的每个部分(页脚、页眉、贝宝表单等),而不是使用<?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>

【问题讨论】:

  • 你是说你想要一个包含$get_header、$get_article、$get_paypal_form和$get_kato函数的php文件吗?然后,您希望每个函数都回显它们的特定部分。最后你想要一个 include()?
  • 这也确实有效!

标签: php html paypal echo


【解决方案1】:

您似乎希望使用 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');

【讨论】:

  • 对我来说全新的不同世界,但答案是有效的,有助于进一步了解可能性。谢谢。
  • 没问题!乐于助人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多