doutuosai3504 2018-04-19 10:40
浏览 67
已采纳

使用PHP解析HTML:获取元素内部的元素[关闭]

I have this structure :

<div id="posts">
<li><a class="title" href="link1">Title 1</a><span class="hour">12:43</span></li>
<li><a class="title" href="link2">Title 2</a><span class="hour">04:43</span></li>
<li><a class="title" href="link3">Title 3</a><span class="hour">15:43</span></li>
<li><a class="title" href="link4">Title 4</a><span class="hour">18:43</span></li>
</div>

I want to get :

$array = ( 
[title => "Title 1", link => "Link1", Date => "Date 1" ], 
[title => "Title 2", link => "Link2", Date => "Date 2" ],
[title => "Title 3", link => "Link3", Date => "Date 3" ],
[title => "Title 4", link => "Link4", Date => "Date 4" ]
)

I want to get that data into an array, can please tell me how to do that with PHP ?

Thanx

  • 写回答

1条回答 默认 最新

  • donglianjiang9321 2018-04-19 10:43
    关注

    Try PHP Simple HTML DOM Parser

    // Create DOM from URL
    $html = file_get_html('http://slashdot.org/');
    
    // Find all article blocks
    foreach($html->find('div.article') as $article) {
        $item['title']     = $article->find('div.title', 0)->plaintext;
        $item['intro']    = $article->find('div.intro', 0)->plaintext;
        $item['details'] = $article->find('div.details', 0)->plaintext;
        $articles[] = $item;
    }
    
    print_r($articles);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?