dongyi8383 2016-03-16 15:36
浏览 58
已采纳

php只从url中检索外部div中的一个字符串

I am trying to link my page to another website in which I can use there div tags in order to keep my site up to date.

I've got some code after some research and it's echoing out just 1 string whereas there are multiple div classes on the page and I would like to echo them all. I am just wondering if this is possible or not?

Here is the current code:

<?php
$url = 'http://www.domain.com';
$content = file_get_contents($url);
$activity = explode( '<div class="class">' , $content );
$activity_second = explode("</div>" , $activity );

echo $activity_second[0];
?>

I can echo $activity_second[0] which will display the first line and $activity_second[1] which will display the second line.

However, I am looking to expand this over to allow all of the div classes on the same page to be put into an array which can then be echo'd out into different parts of a table.

Thank you for your help in advance.

  • 写回答

4条回答 默认 最新

  • doupian9490 2016-03-16 16:01
    关注

    Let me see if I get it straight, you have something like this:

    <div id="another-class"><div class="class">some text 1</div></div>
    <div class="class">some text 2</div>
    <div class="class">some text 3</div>
    <div class="class">some text 4</div>
    <div class="class">some text 5</div>
    <div class="class">some text 6</div>
    

    And you need the text contained the div elements. If this is correct, replace:

    $activity = explode( '<div class="class">' , $content );
    $activity_second = explode("</div>" , $activity );
    

    with this:

    preg_match_all('#<div class="class">(.+?)</div>#', $content, $matches);
    

    In this example, after the function call $matches will have the following:

    Array
    (
        [0] => Array
            (
                [0] => <div class="class">some text 1</div>
                [1] => <div class="class">some text 2</div>
                [2] => <div class="class">some text 3</div>
                [3] => <div class="class">some text 4</div>
                [4] => <div class="class">some text 5</div>
                [5] => <div class="class">some text 6</div>
            )
    
        [1] => Array
            (
                [0] => some text 1
                [1] => some text 2
                [2] => some text 3
                [3] => some text 4
                [4] => some text 5
                [5] => some text 6
            )
    
    )
    

    The data you need is in $matches[1].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch