QueryPath\DOMQuery::attr PHP Method

attr() public method

- If no parameters are specified, this returns an associative array of all name/value pairs. - If both $name and $value are set, then this will set the attribute name/value pair for all items in this object. - If $name is set, and is an array, then all attributes in the array will be set for all items in this object. - If $name is a string and is set, then the attribute value will be returned. When an attribute value is retrieved, only the attribute value of the FIRST match is returned.
See also: removeAttr()
See also: tag()
See also: hasAttr()
See also: hasClass()
public attr ( mixed $name = null, string $value = null ) : mixed
$name mixed The name of the attribute or an associative array of name/value pairs.
$value string A value (used only when setting an individual property).
return mixed If this was a setter request, return the DOMQuery object. If this was an access request (getter), return the string value.
    public function attr($name = null, $value = null)
    {
        // Default case: Return all attributes as an assoc array.
        if (is_null($name)) {
            if ($this->matches->count() == 0) {
                return;
            }
            $ele = $this->getFirstMatch();
            $buffer = array();
            // This does not appear to be part of the DOM
            // spec. Nor is it documented. But it works.
            foreach ($ele->attributes as $name => $attrNode) {
                $buffer[$name] = $attrNode->value;
            }
            return $buffer;
        }
        // multi-setter
        if (is_array($name)) {
            foreach ($name as $k => $v) {
                foreach ($this->matches as $m) {
                    $m->setAttribute($k, $v);
                }
            }
            return $this;
        }
        // setter
        if (isset($value)) {
            foreach ($this->matches as $m) {
                $m->setAttribute($name, $value);
            }
            return $this;
        }
        //getter
        if ($this->matches->count() == 0) {
            return;
        }
        // Special node type handler:
        if ($name == 'nodeType') {
            return $this->getFirstMatch()->nodeType;
        }
        // Always return first match's attr.
        return $this->getFirstMatch()->getAttribute($name);
    }

Usage Example

 /**
  * Extract a single value
  *
  * @param \QueryPath\DOMQuery $item
  * @param array $mapping
  * @param string $value
  * @return string
  */
 protected function _extractValue(\QueryPath\DOMQuery $item, array $mapping, $value = '')
 {
     if ($item) {
         if (!empty($mapping['attr'])) {
             $value = $item->attr($mapping['attr']);
         } elseif (!empty($mapping['innerHTML'])) {
             $value = $item->innerHTML();
         } else {
             $value = $item->text();
         }
     }
     $value = mb_convert_encoding($value, 'UTF-8', 'auto');
     if (!empty($mapping['preg'])) {
         if (preg_match($mapping['preg'], $value, $matches)) {
             $value = isset($matches[1]) ? $matches[1] : $matches[0];
         }
     }
     if (!empty($mapping['wrap'])) {
         $value = str_replace('|', $value, $mapping['wrap']);
     }
     if (!empty($mapping['strtotime'])) {
         $value = strtotime($value);
     }
     if (!isset($mapping['trim']) || !empty($mapping['trim'])) {
         $value = trim($value);
     }
     return $value;
 }