【发布时间】:2015-02-27 11:16:25
【问题描述】:
我正在尝试在我自己的页面上显示来自外部页面的多个 div。 我有以下代码来提取 div。这以一种通用的方式工作,但我希望它更具动态性。
此代码从给定的 div ID 中提取内容并将其显示在我自己的页面上。
<?php
header("Content-Type: text/html; charset=utf-8");
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
//The URL for the external content we want to pull
$html = file_get_contents_curl("https://www.page.com/subdir/");
//parsing all content:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$content = $html;
//The div that includes the content '<div id="divid">'
$first_step = explode( '<div id="ide">' , $content );
$second_step = explode("</div>" , $first_step[1] );
//Do some magic with the URL
$url2 = $second_step[0];
$url3 = $second_step[8];
$url4 = $second_step[16];
$patterns = array(
'#\./opening;jsessionid=.*\?#',
'#<a href=#',
'#span(.*?)>#'
);
$replaces = array(
'https://www.page.com/subdir/opening?',
'<a target="_blank" href=',
'h1>'
);
//Print the final output
///Merge the result into one variable
$final_output =
preg_replace($patterns, $replaces, $url2) .
$second_step[1] . /* Description -- NOTE: By commenting out this you need to change the H1 margin in the style declaration */
$second_step[2] . /* From date */
$second_step[3] . /* To date */
$second_step[4] . /* Company */
$second_step[5] . /* Employment condition (full-time/part-time) */
$second_step[6] . /* Department */
//$second_step[7] .
'<hr>' . /* Horizontal rule */
preg_replace($patterns, $replaces, $url3) .
$second_step[9] . /* Description -- NOTE: By commenting out this you need to change the H1 margin in the style declaration */
$second_step[10] . /* From date */
$second_step[11] . /* To date */
$second_step[12] . /* Company */
$second_step[13] . /* Employment condition (full-time/part-time) */
$second_step[14] . /* Department */
//$second_step[15] .
'<hr>' . /* Horizontal rule */
preg_replace($patterns, $replaces, $url4) .
$second_step[17] . /* Description -- NOTE: By commenting out this you need to change the H1 margin in the style declaration */
$second_step[18] . /* From date */
$second_step[19] . /* To date */
$second_step[20] . /* Company */
$second_step[21] . /* Employment condition (full-time/part-time) */
$second_step[22] . /* Department */
$second_step[22] .
'<hr>'; /* Horizontal rule */
///Convert special chars
$converted = iconv("UTF-8", "UTF-8//TRANSLIT", $final_output);
///Display the final result
echo $converted;
?>
在这段代码中,我有url2、url3 和url4 来定义提取内容的某些部分,以便稍后使用preg_replace 进行修改。我也有 listet $second_step[xx]来定义要显示的内容。
到目前为止,我需要列出 $second_step[xx] 和多个 urlxx 的多个“块”,以便能够使用 divid 显示父 div 的所有子 div。 child-div 没有任何 ID 或类。
我不知道随时要显示多少个 DIV,所以我必须在我的代码中列出很多这样的语句。当我尝试用<hr> 分隔每个 div 时,如果只有一个或没有 DIV 可以显示,我会在页面底部看到很多水平线。
我还希望能够在列中显示 div,例如并排显示两个 div。
我该如何解决这个问题?
编辑:这是我尝试使用的原始数据的示例。
<div id="ide">
<div>
<div class="openingTitle"><a href="./opening;jsessionid=E2A19018E967B4771224A9FA515AFBC0?0-1.ILinkListener-content-contentPanel-openings~view~container-openings~view-0-details"><span style="font-weight:bold;">fagarbeider</span></a></div>
<div class="openingIngress"><p>Avdeling teknisk drift har ledig stilling som fagarbeider vann/avløp.<br/>Fast, 100 %, ledig snarest.</p></div>
<div class="openingDetail"><i>Utlyst: <span>28.01.2015</span></i></div>
<div class="openingDetail"><i>Søknadsfrist: <span style="color:red">01.03.2015</span></i></div>
<div class="openingDetail"><i>Selskap: <span>Randaberg kommune</span></i></div>
<div class="openingDetail"><i>Stillingstype: <span>Fast ansatt</span></i></div>
<div class="openingDetail"><i>Lokasjon: <span>Avd. teknisk drift</span></i></div>
<div class="openingDetail">
</div>
<div>
<div class="openingTitle"><a href="./opening;jsessionid=E2A19018E967B4771224A9FA515AFBC0?0-1.ILinkListener-content-contentPanel-openings~view~container-openings~view-0-details"><span style="font-weight:bold;">fagarbeider2</span></a></div>
<div class="openingIngress"><p>Avdeling teknisk drift har ledig stilling som fagarbeider vann/avløp.<br/>Fast, 100 %, ledig snarest.</p></div>
<div class="openingDetail"><i>Utlyst: <span>28.01.2015</span></i></div>
<div class="openingDetail"><i>Søknadsfrist: <span style="color:red">01.03.2015</span></i></div>
<div class="openingDetail"><i>Selskap: <span>Randaberg kommune</span></i></div>
<div class="openingDetail"><i>Stillingstype: <span>Fast ansatt</span></i></div>
<div class="openingDetail"><i>Lokasjon: <span>Avd. teknisk drift</span></i></div>
<div class="openingDetail">
</div>
</div>
谢谢!
【问题讨论】:
-
id 就是id,一页上不能有多个相同id的元素。
-
感谢@n-dru,我更仔细地查看了我正在提取的原始数据,并编辑了我的问题。只有一个具有指定 ID 的 div,但是这个 div 有许多没有任何 ID 或类的子 div。
-
但是那些子div的子div有类....
-
你应该使用 DOMDocument 或类似的东西,而不是尝试做一堆字符串操作和正则表达式。
-
感谢@MikeBrant 的回答,我对
DOMDocument不熟悉。在使用 Phlanger 作为 ASP 的 PHP 编译器时,我在提取 https url 时遇到了问题。因此,我不得不添加一个 curl 函数和一些 DOM 命令来管理提取数据。但这就是我迄今为止对 DOM 所做的一切。我已将此添加到最后一篇文章的代码中。我如何使用 DOM 在这里获得我想要的结果?