simple_html_dom_node::text PHP Method

text() public method

get dom node's plain text
public text ( )
    function text()
    {
        if (isset($this->_[HDOM_INFO_INNER])) {
            return $this->_[HDOM_INFO_INNER];
        }
        switch ($this->nodetype) {
            case HDOM_TYPE_TEXT:
                return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);
            case HDOM_TYPE_COMMENT:
                return '';
            case HDOM_TYPE_UNKNOWN:
                return '';
        }
        if (strcasecmp($this->tag, 'script') === 0) {
            return '';
        }
        if (strcasecmp($this->tag, 'style') === 0) {
            return '';
        }
        $ret = '';
        // In rare cases, (always node type 1 or HDOM_TYPE_ELEMENT - observed for some span tags, and some p tags) $this->nodes is set to NULL.
        // NOTE: This indicates that there is a problem where it's set to NULL without a clear happening.
        // WHY is this happening?
        if (!is_null($this->nodes)) {
            foreach ($this->nodes as $n) {
                $ret .= $this->convert_text($n->text());
            }
        }
        return $ret;
    }

Usage Example

 /**
  * @param Listing $listing
  * @param string $type
  * @param string $basis
  * @param \DateTime $dateStart
  * @param \DateTime $dateEnd
  * @param string $currency
  * @param \simple_html_dom_node|array $priceDom
  * @param \simple_html_dom_node|array $priceNotes
  *
  * @throws \UnexpectedValueException
  */
 protected function parsePriceByType(Listing $listing, $type, $basis, \DateTime $dateStart, \DateTime $dateEnd, $currency, $priceDom, $priceNotes)
 {
     if (!isset($priceDom)) {
         return;
     }
     $price = '';
     foreach ($priceDom->nodes as $node) {
         if ($node->tag == 'text') {
             $price .= trim($node->text());
         }
     }
     if (empty($price)) {
         return;
     }
     $price = str_replace(array('$', ','), '', trim($price));
     if (!is_numeric($price)) {
         throw new \UnexpectedValueException(sprintf('Price ammount not found in "%s"', trim($priceDom->text())));
     }
     $priceNotes = trim($priceNotes->text());
     $listingPrice = $listing->getPrice($type, $dateStart, $dateEnd);
     if (!isset($listingPrice)) {
         $listingPrice = new ListingPrice();
         $listing->addPrice($listingPrice);
     }
     $listingPrice->setType($type)->setBasis($basis)->setDateStart($dateStart)->setDateEnd($dateEnd)->setPrice($price)->setCurrency($currency)->setNotes($priceNotes);
 }
All Usage Examples Of simple_html_dom_node::text