Find div with class using PHP Simple HTML DOM Parser

Find div with class using PHP Simple HTML DOM Parser

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Find div with class using PHP Simple HTML DOM Parser

When working with simple_html_dom, a common stumbling block arises when trying to select elements based on multiple classes. The core issue in your scenario stems from how the library parses selector strings versus standard CSS syntax. You attempted to use $html->find('div[.ClearBoth Box]') expecting it to match an element with both ClearBoth and Box as classes.

In reality, Simple HTML DOM interprets [.class1 class2] as a single attribute selector where the value of the class attribute must literally equal the string .ClearBoth Box. It does not automatically split the space-separated class names into an intersection query. If your source code actually contains class="ClearBoth Box", this specific syntax will fail to match because the library is looking for a node where the entire class attribute is exactly that string, but combined with other logic that might be confusing your results.

The correct approach is to use the CSS-like class selector syntax supported by Simple HTML DOM: div.ClearBoth.Box. This tells the parser to find a <div> element that possesses both classes simultaneously. However, there is a critical nuance in your debug output that suggests a deeper parsing problem: you are finding nodes where the class attribute only contains ClearBoth, and these nodes appear as children of other elements.

Looking at your printed array, you see objects with [tag] => br or div inside a parent <div class="SocialMedia">. This indicates that the parser is returning the wrong scope or that the selector is matching partial attribute values incorrectly due to how the DOM tree was built from the raw string.

Correcting the Selector Syntax

To reliably find elements with multiple classes, drop the brackets and dots around the specific classes when chaining them. Instead of trying to force a regex-like pattern inside brackets, rely on standard CSS class concatenation.

Update your code as follows:

// Load the HTML content from cURL
$cl = curl_exec($curl);  
$html = new simple_html_dom();
$html->load($cl);

// Use standard CSS class chaining for multiple classes
$divs = $html->find('div.ClearBoth.Box');

If this returns an empty array, verify that the source code you are fetching actually contains the exact string class="ClearBoth Box" (with a space) and not a different combination. Sometimes server-side rendering or minification alters class names slightly. If you need to find elements where the class list includes these words but isn't strictly defined by them, you might need to iterate over the collection and check individual attributes manually.

Handling Parsing Artifacts

The output you provided shows nested objects with [parent] pointing to a different div. This structure is normal for Simple HTML DOM; every node knows its parent. The confusion comes from seeing tag => br inside your results. If your selector matches a div, why are you getting br elements?

This usually happens if the source HTML is malformed or if the parser is traversing into child nodes unexpectedly. Ensure you are casting the result to an array properly and checking if $divs is actually empty before trying to access it.

Additionally, Simple HTML DOM is a single-threaded C extension. It does not maintain a full Document Object Model (DOM) in memory like DOMDocument. When you call load(), it parses the string into a tree structure where every node knows its children and parents. If your selector matches a parent div that contains a br tag, but the logic inside your loop assumes the result is a leaf node containing the text you want, you will see unexpected data.

Extracting the Content

Once you have the correct $divs array populated with only the target <div> elements, iterate through them to extract the inner HTML or text.

foreach ($divs as $div) {
    // Get the inner HTML of the matched div
    $content = $div->innertext; 
    
    // Or get just the text content, ignoring tags
    $text = $div->plaintext; 
    
    echo "Found: " . trim($content) . "\n";
}

If you are scraping specific data like ratings or dates, remember that innertext includes nested tags. If you want raw text without HTML entities, use plaintext. However, note that plaintext converts all HTML entities back to their original characters (e.g., &amp; becomes &), which is often desired for display but requires sanitization if you plan to echo this directly into a view.

When Simple HTML DOM Isn't Enough

While simple_html_dom is excellent for quick scraping tasks due to its speed and ease of use, it lacks the robustness of standard XML parsers like DOMDocument or LibXML. If your target site uses complex JavaScript-rendered content (like React or Vue apps), curl_exec will only fetch the initial HTML shell, often missing dynamic elements entirely. In such cases, switching to a headless browser solution is necessary.

For large-scale projects where maintainability and strict typing are priorities, consider building a scraper on top of Laravel. The ecosystem offers tools to manage cURL sessions, handle rate limiting, and parse results consistently across different views. While simple_html_dom works well for ad-hoc scripts, integrating it into a structured application allows you to isolate parsing logic, cache results, and handle errors gracefully without rewriting the whole parser logic every time.

The key takeaway is mastering the selector syntax first. Once you switch from [.Class1 Class2] to .Class1.Class2, the parser behavior usually aligns with expectations, isolating the specific div you intend to scrape.

Our network