simple_html_dom_node::find PHP Method

find() public method

PaperG - added ability for find to lowercase the value of the selector.
public find ( $selector, $idx = null, $lowercase = false )
    function find($selector, $idx = null, $lowercase = false)
    {
        $selectors = $this->parse_selector($selector);
        if (($count = count($selectors)) === 0) {
            return array();
        }
        $found_keys = array();
        // find each selector
        for ($c = 0; $c < $count; ++$c) {
            // The change on the below line was documented on the sourceforge code tracker id 2788009
            // used to be: if (($levle=count($selectors[0]))===0) return array();
            if (($levle = count($selectors[$c])) === 0) {
                return array();
            }
            if (!isset($this->_[HDOM_INFO_BEGIN])) {
                return array();
            }
            $head = array($this->_[HDOM_INFO_BEGIN] => 1);
            // handle descendant selectors, no recursive!
            for ($l = 0; $l < $levle; ++$l) {
                $ret = array();
                foreach ($head as $k => $v) {
                    $n = $k === -1 ? $this->dom->root : $this->dom->nodes[$k];
                    //PaperG - Pass this optional parameter on to the seek function.
                    $n->seek($selectors[$c][$l], $ret, $lowercase);
                }
                $head = $ret;
            }
            foreach ($head as $k => $v) {
                if (!isset($found_keys[$k])) {
                    $found_keys[$k] = 1;
                }
            }
        }
        // sort keys
        ksort($found_keys);
        $found = array();
        foreach ($found_keys as $k => $v) {
            $found[] = $this->dom->nodes[$k];
        }
        // return nth-element or array
        if (is_null($idx)) {
            return $found;
        } else {
            if ($idx < 0) {
                $idx = count($found) + $idx;
            }
        }
        return isset($found[$idx]) ? $found[$idx] : null;
    }

Usage Example

 /**
  * Prepare components (set classes to nodes etc.)
  * @param ComponentAbstract $component
  * @param \simple_html_dom_node $node
  */
 protected function beforeHandle(ComponentAbstract $component, \simple_html_dom_node $node)
 {
     $this->attachAngularApp();
     if ($class = $component->getOption('class')) {
         if (is_array($class)) {
             $class = implode(' ', $class);
         } elseif (is_callable($class)) {
             $class = $class();
         }
         if (is_string($class)) {
             $node->setAttribute('class', $class);
         }
     }
     $id = $component->getOption('id');
     if (!$id) {
         $id = $node->getAttribute('id');
     }
     if (!$id) {
         $id = $this->getId($node->getAttribute('data-component'));
     }
     if ($node->getAttribute('data-component') === 'likeGate') {
         $node->setAttribute('ng-init', 'likeGateEnabled = 1');
         $node->setAttribute('ng-show', 'likeGateEnabled');
         $header = $node->find('#likeGate-header', 0);
         $message = $node->find('#likeGate-message', 0);
         if ($header) {
             $header->setAttribute('ng-bind', 'likeGateHeader');
         }
         if ($message) {
             $message->setAttribute('ng-bind', 'likeGateMessage');
         }
         $node->setAttribute('ng-model', 'pageData.' . $id);
     }
 }
All Usage Examples Of simple_html_dom_node::find