Callum Whyte
Callum Whyte

Reputation: 2429

Scraping <li> contents with PHP dom

how I could use PHP dom screen scraping to pull the contents of an HTML tag called

<li style="margin-top:10px">

positioned in one of my pages?

I want to get all the contents of the <li> tag and display it as html code.

Upvotes: 0

Views: 847

Answers (2)

Michael Berkowski
Michael Berkowski

Reputation: 270637

Use simpleXML and xpath. Supposing your HTML is all stored in the string $html, this may fit your need:

// Load your html from a file
$html = $file_get_contents("/path/to/page.html");
$xml = simplexml_load_string($html);

$li = $xml->xpath("//li[@style='margin-top:10px]");
echo $li->asXML();

Upvotes: 4

Gerben
Gerben

Reputation: 16825

$html='<li style="margin-top:10px">hello <b>World</b></li>';
if( preg_match('|<li style="margin-top:10px">(.*?)</li>|', $html, $matches) )
{
  $licontent = $matches[1];
}

Upvotes: 1

Related Questions