<?php

class DevelopersUtils {

    public function extract($url, $value) {

        $dom = new DOMDocument();

        $file = 'content.txt';
        //$current = file_get_contents($url);
        $current = CurlTool::downloadFile($url, $file);
        //file_put_contents($file, $current);

        @$dom->loadHTMLFile($current);

        //use DOMXpath to navigate the html with the DOM
        $dom_xpath = new DOMXpath($dom);

        $results = $dom_xpath->query('//td1'); // (or any offending element)
        foreach ($results as $invalidNode) {
            $parentNode = $invalidNode->parentNode;
            $children = $invalidNode->childNodes;
            foreach ($children as $childNode) {
                $parentNode->insertBefore($childNode, $invalidNode);
            }
            $parentNode->removeChild($invalidNode);
        }
        
       
        //die();
        
        //$dom_xpath = new DOMXpath($dom);
        
        $elements = $dom_xpath->query("//*[text()[contains(., '" . $value . "')]]");
        var_dump($elements);
        if (!is_null($elements)) {

            foreach ($elements as $element) {
                var_dump($element);
                echo "\n1.[" . $element->nodeName . "]\n";

                $nodes = $element->childNodes;
                foreach ($nodes as $node) {
                    if (($node->nodeValue != null) && ($node->nodeValue === $value)) {
                        echo '2.' . $node->nodeValue . "\n";
                        $xpath = preg_replace("/\/text\(\)/", "", $node->getNodePath());
                        echo '3.' . $xpath . "\n";
                    }
                }
            }
        }
    }
}

$o = new DevelopersUtils();

$o->extract('http://w...content-available-to-author-only...e.ro/preturi_canyon_4-in-1_stylus_bundle_pack_cng-ds03_199062.htm','Canyon 4-in-1 Stylus bundle pack CNG-DS03');