-4

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

user3134277
  • 347
  • 4
  • 14

1 Answers1

1

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);
Hamza Alayed
  • 635
  • 5
  • 17
  • 1
    Sorry! I did not explain it full, you have to download the library then include it to the page you want to pasre. I will update the answer. – Hamza Alayed Apr 19 '18 at 10:49
  • Warning: file_get_contents(): stream does not support seeking in C:\xampp\htdocs\gg\simplehtmldom_1_5\simple_html_dom.php on line 75 Warning: file_get_contents(): Failed to seek to position -1 in the stream in C:\xampp\htdocs\gg\simplehtmldom_1_5\simple_html_dom.php on line 75 – user3134277 Apr 19 '18 at 10:49
  • 1
    check this answer https://stackoverflow.com/questions/42685814/file-get-contents-stream-does-not-support-seeking-when-was-php-behavior-abo – Hamza Alayed Apr 19 '18 at 10:51
  • thanx a lot !!! it works :) – user3134277 Apr 19 '18 at 11:02
  • nice to hear that – Hamza Alayed Apr 19 '18 at 11:03