QueryPath\DOMQuery::text PHP Method

text() public method

Get or set the text contents of a node.
See also: html()
See also: xml()
See also: contents()
public text ( string $text = null ) : mixed
$text string If this is not NULL, this value will be set as the text of the node. It will replace any existing content.
return mixed A DOMQuery if $text is set, or the text content if no text is passed in as a pram.
    public function text($text = null)
    {
        if (isset($text)) {
            $this->removeChildren();
            foreach ($this->matches as $m) {
                $m->appendChild($this->document->createTextNode($text));
            }
            return $this;
        }
        // Returns all text as one string:
        $buf = '';
        foreach ($this->matches as $m) {
            $buf .= $m->textContent;
        }
        return $buf;
    }

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;
 }
All Usage Examples Of QueryPath\DOMQuery::text