Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

christopher's avatar

DomXPath / DOMDocument search document for keywords and put out

Hi guys -

i am writing a little function to search an html file. Currently the function searches for the keyword "effizienzklasse" but the function should search multiple keywords such as "effizienzklasse" and "energie" and "verbrauch" and return it as an array.

How can i pass multiple keywords to the DomXPath ?

    private function getCop()
    {
        $url = "http://content.copmedia.de/de/1373B284BC381890049E92D324F56DE0S15101503.html";

        $result = array();
        $dom = new DOMDocument;
        $dom->loadHTML(file_get_contents($url));

        // find all <div>'s with the class lineData using DomXPath :
        $xpath = new DomXPath($dom);
        $lineDatas = $xpath->query('//div[contains(@class,"lineData")]');

        // add all lineData <div>'s containing the keyword to the $result array :
        foreach($lineDatas as $lineData) {
            if (strpos(strtolower($lineData->nodeValue), 'effizienzklasse') !== false) {
                $result[] = $lineData;
            }
        }

        return $dom->saveHTML($lineData);
    }

}

Hope someone could help me .. :)

0 likes
1 reply
aktolman's avatar

Hiya - did you get this sorted? Are you just trying to include multiple search items in your Xpath?

If so its: // to contain multiples //div[contains(@class,"lineData") and contains(@class,"lineData2")] OR // to contain either or //div[contains(@class,"lineData") or contains(@class,"lineData2")]

Please or to participate in this conversation.