从外部源获取多个DIV


Fetch multiple DIVs from external source

我正试图在自己的页面上显示来自外部页面的多个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;
?>

在这段代码中,我有url2url3url4来定义提取内容的某些部分,以便稍后使用preg_replace进行修改。我还有列表集$second_step[xx]来定义要显示的内容。

到目前为止,我需要列出$second_step[xx]和多个urlxx的多个"块",才能用divid显示父div的所有子div。子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:&nbsp;<span>28.01.2015</span></i></div>
        <div class="openingDetail"><i>Søknadsfrist:&nbsp;<span style="color:red">01.03.2015</span></i></div>
        <div class="openingDetail"><i>Selskap:&nbsp;<span>Randaberg kommune</span></i></div>
        <div class="openingDetail"><i>Stillingstype:&nbsp;<span>Fast ansatt</span></i></div>
        <div class="openingDetail"><i>Lokasjon:&nbsp;<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:&nbsp;<span>28.01.2015</span></i></div>
        <div class="openingDetail"><i>Søknadsfrist:&nbsp;<span style="color:red">01.03.2015</span></i></div>
        <div class="openingDetail"><i>Selskap:&nbsp;<span>Randaberg kommune</span></i></div>
        <div class="openingDetail"><i>Stillingstype:&nbsp;<span>Fast ansatt</span></i></div>
        <div class="openingDetail"><i>Lokasjon:&nbsp;<span>Avd. teknisk drift</span></i></div>
        <div class="openingDetail">
        </div>
    </div>

谢谢!

您可以尝试使用regex:提取它

preg_match_all('{<div[^<>]*[^<>]*>(?<content>.*?)</div>}', $content, $matches);
$array_of_contents = $matches[0];

现在$array_of_contents是一个数组,包含那些div中的所有内容。当然,它只涉及那些内部div,它们必须在一个级别上。