How to parse a list using simple html dom

Sumit

I have an html code, and I'm facing a problem parsing a data out of this html specifically from the part given below:

<li id=xyz>
  John Johnson
<sup>1<sup>
","
</li>

I want to extract "John Johnson" out of this list and nothing else. Not sure how to do so. Thanks.

Enissay

find('text') is what you're after. It returns all text blocks found in the source.

Based on your example here's a working code:

// Test data
$input = <<<_DATA_
    <li id=xyz>
      John Johnson
    <sup>1<sup>
    ","
    </li>
_DATA_;

//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load($input);

// >> Long answer
echo "Long answer:<br/>";

// Search all text nodes inside the target node
$search = $html->find('li#xyz text');

// Loop through each node and print it
foreach( $search as $i => $txt ) {
    // No need to specify "->plaintext" since the content is already in plain text here
    echo "$i => " . $txt->plaintext . "<br/>";
}

// >> Short answer
echo "<hr>";
echo "Short answer:<br/>";

// Specifying the index (0th here) returns the Nth element from the array containing all search results
echo $html->find('li#xyz text', 0)->plaintext;

// Clear DOM object
$html->clear();
unset($html);

OUTPUT:

Long answer:
0 => John Johnson 
1 => 1
2 => "," 
3 => 
-------------------
Short answer:
John Johnson

For more details check the Manual

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

parse a html heterogeneous list in php using simple_html_dom

How to parse multiple elements in portions for html via Simple Html Dom

Parse output using PHP Simple HTML DOM parser

DOMXpath/DOMDocument - How to parse HTML dom elements not only with simple text

Get all HTML list element using Simple HTML Dom

How to parse user defined html attributes using html DOM with '-' in that attribute?

How to HTML parse a URL list using python

Using Simple HTML Dom PHP

How to parse HTML file using PHP DOM module?

How to import multiple urls using simple_html_dom in PHP?

Parse website and save a specific DIV using 'PHP Simple HTML DOM Parser'

How to parse data-extension attribute with PHP Simple HTML DOM Parser

How to create a list with formatted items using DOM instead on HTML concatenation?

Parse all items with PHP Simple HTML DOM Parser

Cant't Parse Web Pages with PHP Simple HTML DOM Parser

How to parse an HTML document for an element list of 'tagname' using Jsoup?

PHP error using simple_html_dom

Scraping data using PHP Simple HTML DOM

Using a proxy with PHP Simple HTML DOM Parser

How to convert simple html list to multilevel using jquery

How Can I get values into html tags using Simple HTML Dom?

How use simple_html_dom in laravel

How to style the Simple_HTML_DOM output

How to Insert Data In MYSQL Database From A For Loop As a Single Query Using Simple HTML Dom

How to get tag's attribute using PHP simple HTML DOM parser

How to skip repeated content from searching using simple html dom parser

How to crawl the title and content of a bulletin board using PHP's Simple HTML DOM?

Get h2 html using Simple HTML DOM Parser

Scrape HTML & count children using Simple HTML DOM