Get Element by ClassName with DOMdocument() Method

get class name javascript
javascript get child element by class
domdocument get element by class
get element by class jquery
getelementsbyclassname style
php domdocument get element by tag name
getelementsbyclassname innerhtml
getelementsbyclassname multiple classes

Here is what I am trying to achieve : retrieve all products on a page and put them into an array. Here is the code I am using :

$page2 = curl_exec($ch);
$doc = new DOMDocument();
@$doc->loadHTML($page2);
$nodes = $doc->getElementsByTagName('title');
$noders = $doc->getElementsByClassName('productImage');
$title = $nodes->item(0)->nodeValue;
$product = $noders->item(0)->imageObject.src;

It works for the $title but not for the product. For info, in the HTML code the img tag looks like this :

<img alt="" class="productImage" data-altimages="" src="xxxx">

I have been looking at this (PHP DOMDocument how to get element?) but I still don't understand how to make it work.

PS : I get this error :

Call to undefined method DOMDocument::getElementsByclassName()

I finally used the following solution :

    $classname="blockProduct";
    $finder = new DomXPath($doc);
    $spaner = $finder->query("//*[contains(@class, '$classname')]");

Get Element by ClassName with DOMdocument() Method, I finally used the following solution : $classname="blockProduct"; $finder = new DomXPath($doc); $spaner  Get Element by ClassName with DOMdocument() Method. Call to undefined method DOMDocument:: How is it possible to get elements by class name in PHP (DOM

https://stackoverflow.com/a/31616848/3068233

Linking this answer as it helped me the most with this problem.

function getElementsByClass(&$parentNode, $tagName, $className) {
    $nodes=array();

    $childNodeList = $parentNode->getElementsByTagName($tagName);
    for ($i = 0; $i < $childNodeList->length; $i++) {
        $temp = $childNodeList->item($i);
        if (stripos($temp->getAttribute('class'), $className) !== false) {
            $nodes[]=$temp;
        }
    }

    return $nodes;
}

Theres the code and heres the usage

$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadHTML($html);
$content_node=$dom->getElementById("content_node");

$div_a_class_nodes=getElementsByClass($content_node, 'div', 'a');

HTML DOM getElementsByClassName() Method, HTML DOM getElementsByClassName() Method. ❮ Document Object. Example. Get all elements with the specified class name: var x =  The getElementsByClassName () method returns a collection of an element's child elements with the specified class name, as a NodeList object. The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers. The index starts at 0. Tip: You can use the length property of the NodeList object to determine the number

function getElementsByClassName($dom, $ClassName, $tagName=null) {
    if($tagName){
        $Elements = $dom->getElementsByTagName($tagName);
    }else {
        $Elements = $dom->getElementsByTagName("*");
    }
    $Matched = array();
    for($i=0;$i<$Elements->length;$i++) {
        if($Elements->item($i)->attributes->getNamedItem('class')){
            if($Elements->item($i)->attributes->getNamedItem('class')->nodeValue == $ClassName) {
                $Matched[]=$Elements->item($i);
            }
        }
    }
    return $Matched;
}

// usage

    $dom = new \DOMDocument('1.0'); 
    @$dom->loadHTML($html);
    $elementsByClass = getElementsByClassName($dom, $className, 'h1');

Document.getElementsByClassName(), The getElementsByClassName() method returns a collection of an element's child elements with the The class name of the child elements you want to get. names is a string representing the class name(s) to match; multiple class names are separated by whitespace; getElementsByClassName can be called on any element, not only on the document. The element on which it is called will be used as the root of the search. Examples. Get all elements that have a class of 'test': document

Element.getElementsByClassName(), The getElementsByClassName method of Document interface returns an array-​like object of all child elements which have all of the given class name(s). Get all elements that have a class of 'test', inside of an element that has the ID of DOM The definition of 'document.getElementsByClassName' in that  The method is not supported by PHPs DOMDocument. It can be emulated by Xpath. Any CSS3 selector that does not return pseudo elements can be converted into an Xpath expression. So to match a CSS class attribute you have to understand how it works. A CSS class is a token attribute. It contains several class names separated by whitespaces.

DOMDocument::getElementsByTagName - Manual, The Element method getElementsByClassName() returns a live current state of the DOM tree rooted at the element on which the function was called. we just provide that class name when calling getElementsByClassName() : To find elements whose class lists include both the red and test classes: The getElementsByName() method returns a collection of all elements in the document with the specified name (the value of the name attribute), as a NodeList object. The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers.

Parsing HTML files with DOMDocument and DOMXpath < PHP, public DOMDocument::getElementsByTagName ( string $name ) : DOMNodeList DOMDocument::getElementsByTagNameNS() - Searches for all elements with This is a very simplistic way to traverse xml nodes and childnodes using the DOMDocument class And this is how I get elements by attribute and its value. The getElementsByTagName() method returns a collection of an elements's child elements with the specified tag name, as a NodeList object. The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers. The index starts at 0.

Comments
  • This question is weird, as OP makes up its own not-exist method.
  • A more correct xpath variant is presented as an answer in a duplicated question: Call to undefined method DOMDocument::getElementsByClassName()
  • watch out, the stripos check in that function can result in false positives.. If you have a class like FormRowHeader then it would still return true for FormRow.