Strip html tags and render a text-only version.

@param   string  $body

@return  string
977468
protected function renderTextBody($body)
    {
        $body = html_entity_decode(
            trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s', '', $body))), ENT_QUOTES
        );

        if (empty($body)) {
          $body = 'To view this email, open it an email client that supports HTML.';
        }

        return $body;
    }
608260
public function url($body)
    {
        // We want to make sure each time we use this the dom crawler doesn't
        // still have the content from the last time it was used
        //
        // There is a method to "clear" the dom crawler, but it is either buggy
        // in this version or doesn't work like that. Instead, we clone the
        // instance that was injected and use that.
        $crawler = clone $this->crawler;

        // Add the html to the body
        // probably should do some validation here
        $crawler->addContent($body);

        foreach ($this->tags as $type => $property) {
            $tag       = $property[0];
            $attribute = $property[1];

            $tag = $crawler->filter($tag);

            if (count($tag) == 0) {
                continue;
            }

            $url = $tag->attr($attribute);

            if ($url) {
                /*
                 * If we are using the http-refresh tag, then we need to rip out
                 * the actual url from the tag.
                 *
                 * A http-refresh tag generally looks like this:
                 *   <meta http-equiv="refresh" content="3;url=http://foobar.com" />
                 *
                 * We want to only have the part after the url= piece.
                 */
                if ($type == 'http-refresh') {
                    $re = '/url=(.*)/i';

                    preg_match($re, $url, $matches);

                    $url = $matches[1];

                    // If this fails, we try a different tag
                    if (!$url) {
                        continue;
                    }

                    return new Url($url, true);
                }

                return new Url($url, false);
            }
        }

        return false;
    }
57377
public function body($body = null)
    {
        if ($body === null) {
            $body = $this->getBody();

            return $body ? $body->__toString() : '';
        }
        if (is_array($body)) {
            $formData = new FormData();
            $formData->addMany($body);
            $this->addHeaders(['Content-Type' => $formData->contentType()]);
            $body = (string)$formData;
        }
        $stream = new Stream('php://memory', 'rw');
        $stream->write($body);
        $this->stream = $stream;

        return $this;
    }
124217
public function render($text)
    {
        $text = trim(str_replace(array("\r\n", "\r"), "\n", $text), "\n");

        foreach ($this->rules as $regex => $replacement) {
            if (is_callable($replacement)) {
                $text = preg_replace_callback($regex, $replacement, $text);
            } else {
                $text = preg_replace($regex, $replacement, $text);
            }
        }

        return trim($text);
    }
261401
public static function Image($Body) {
      if (is_string($Body)) {
         $Image = @unserialize($Body);
      
         if (!$Image) 
            return Gdn_Format::Html($Body);
      }
      
      $Url = GetValue('Image', $Image);
      $Caption = Gdn_Format::PlainText(GetValue('Caption', $Image));
      return '<div class="ImageWrap">'
         .'<div class="Image">'
            .Img($Url, array('alt' => $Caption, 'title' => $Caption))
         .'</div>'
         .'<div class="Caption">'.$Caption.'</div>'
      .'</div>';
   }
560793
private function parseBody(Node $body): PerLineNode
    {
        $prefix  = '';                       // Text before the (possibly) multi line expression
        $postfix = '';                       // Text before the (possibly) multi line expression
        $lineno  = $body->getTemplateLine(); // The line number where we found the {% perline %} tag

        // If the body does not contain a list of tags, the body itself is the
        // only useful content of the perline tags, so we return only the body
        // tag. This is the case when the perline tags could be removed without
        // an effect in the generated code.
        if (count($body) === 0) {
            return new PerLineNode($body, '', '', $lineno, $this->getTag());
        }

        // Get all the nodes as array, because it will be modified.
        $nodes = $body->getIterator()->getArrayCopy();

        // Check for prefix, the first node should be a text node to have a
        // prefix.
        $first = reset($nodes);
        if ($first instanceof TextNode) {
            $prefix = $first->getAttribute('data');
            array_shift($nodes);
        }

        // Check for postfix, the last node should be a text node to have a
        // postfix.
        $last = end($nodes);
        if ($last instanceof TextNode) {
            $postfix = rtrim($last->getAttribute('data'));
            array_pop($nodes);
        }

        // After we ditched the prefix and postfix there could only be one node
        // left, ready to be returned and used directly. If there are multiple
        // we wrap those in a Twig Node.
        if (count($nodes) === 1) {
            $nodes = current($nodes);
        } else {
            $nodes = new Node($nodes);
        }

        // Return the new structured node.
        return new PerLineNode($nodes, $prefix, $postfix, $lineno, $this->getTag());
    }
731631
public static String stripHtmlOverhead(String strText)
    {
        strText = MenuParser.stripTopLevelTag(strText, "html");
        strText = MenuParser.stripTopLevelTag(strText, "body");
        return strText;
    }
118475
public static function decodeChunkedBody($body)
    {
        $decBody = '';
        
        while (trim($body)) {
            if (! preg_match("/^([\da-fA-F]+)[^\r\n]*\r\n/sm", $body, $m)) {
                require_once 'Zend/Http/Exception.php';
                throw new Zend_Http_Exception("Error parsing body - doesn't seem to be a chunked message");
            }

            $length = hexdec(trim($m[1]));
            $cut = strlen($m[0]);

            $decBody .= substr($body, $cut, $length);
            $body = substr($body, $cut + $length + 2);
        }

        return $decBody;
    }
1080809
public function stripBom( $body )
    {
        if ( substr( $body, 0, 3 ) === "\xef\xbb\xbf" )  // UTF-8
        {
            $body = substr( $body, 3 );
        } else {
            if ( substr( $body, 0, 4 ) === "\xff\xfe\x00\x00" || substr( $body, 0, 4 ) === "\x00\x00\xfe\xff" )  // UTF-32
            {
                $body = substr( $body, 4 );
            } else {
                if ( substr( $body, 0, 2 ) === "\xff\xfe" || substr( $body, 0, 2 ) === "\xfe\xff" )  // UTF-16
                {
                    $body = substr( $body, 2 );
                }
            }
        }

        return $body;
    }
909833
private function retrieveErrorMessage($body)
    {
        if (empty($body) || false === strpos($body, '<?xml')) {
            return '';
        }
        $xml = simplexml_load_string($body);
        if (isset($xml->Message)) {
            return strval($xml->Message);
        }
        return '';
    }
929904
protected static function stripTags($content)
    {
        $content = preg_replace('@<([^>]+(<|\z))@msi', '##lt##$1', $content);
        $content = strip_tags($content);
        // unescape < that are not used to open a tag
        return str_replace('##lt##', '<', $content);
    }
255689
static function TextToHtml($txt) 
	{
		//Kills double spaces and spaces inside tags.
		while( !( strpos($txt,'  ') === FALSE ) ) $txt = str_replace('  ',' ',$txt);
		$txt = str_replace(' >','>',$txt);
		$txt = str_replace('< ','<',$txt);
	
		//Transforms accents in html entities.
		$txt = htmlentities($txt);
	
		//We need some HTML entities back!
		$txt = str_replace('&quot;','"',$txt);
		$txt = str_replace('&lt;','<',$txt);
		$txt = str_replace('&gt;','>',$txt);
		$txt = str_replace('&amp;','&',$txt);
	
		//Ajdusts links - anything starting with HTTP opens in a new window
		//$txt = str_ireplace("<a href=\"http://","<a target=\"_blank\" href=\"http://",$txt);
		//$txt = str_ireplace("<a href=http://","<a target=\"_blank\" href=http://",$txt);

		
		//Basic formatting
		$eol = ( strpos($txt,"\r") === FALSE ) ? "\n" : "\r\n";
		$html = '<p>'.str_replace("$eol$eol","</p><p>",$txt).'</p>';
		$html = str_replace("$eol","<br />\n",$html);
		$html = str_replace("</p>","</p>\n\n",$html);
		$html = str_replace("<p></p>","<p>&nbsp;</p>",$html);
	
		//Wipes <br> after block tags (for when the user includes some html in the text).
		$wipebr = Array("table","tr","td","blockquote","ul","ol","li");
	
		for($x = 0; $x < count($wipebr); $x++) 
		{
			$tag = $wipebr[$x];
			$html = str_ireplace("<$tag><br />","<$tag>",$html);
			$html = str_ireplace("</$tag><br />","</$tag>",$html);
		}
	
		return $html;
	}
788834
public function strip($html = false, $echo = false) {
        if ($this->_stripped) {
            if ($echo) {
                echo $this->_stripped;
            }

            return $this->_stripped;
        }

        $this->_triggerHook('startup');

        $string = $this->_triggerHook('beforeStrip', $this->_string);

        if ($this->_isParseable($string)) {
            $string = $this->_strip($this->_extractChunks($string));
        } else {
            $string = $this->convertLineBreaks($string);
        }

        $string = $this->_triggerHook('afterStrip', $string);

        if (!$html) {
            $string = preg_replace('/<br\/?>/', "\n", $string); // convert back
            $string = strip_tags($string);
        }

        $this->_stripped = $this->cleanNewlines($string);

        if ($echo) {
            echo $this->_stripped;
        }

        return $this->_stripped;
    }
1263582
public function getBodyData($body) {
    $module_path = $this->moduleHandler->getModule('govcms8_default_content')->getPath();
    if (!empty($body)) {
      if (is_array($body) && !empty($body['file'])) {
        $file = $body['file'];
        $body_path = $module_path . '/import/html_body/' . $file;
        $body_html = file_get_contents($body_path);
        if ($body_html !== FALSE) {
          return $body_html;
        }
      }
      else {
        return $body;
      }
    }
  }
735707
public static function strip_html($str) {
        $str = preg_replace('/^\s*(.*?)\s*$/im', '$1', $str);
        $str = preg_replace('/^\/\/(.*?)$/im', '', $str);
        $str = preg_replace('/\r|\n/i', ' ', $str);
        $str = preg_replace('/\>\s*(.*?)\s*\</im', '>$1<', $str);
        $str = preg_replace('/\s{2,}/i', ' ', $str);
        return $str;
    }
1011936
public static function renderTable(
        array $head = [], 
        array $body = [], 
        array $attributes = []
    ) : string 
    {
        $strHead = "";
        $strBody = "";
        
        foreach ($head as $thRow) {
            $strHead .= self::renderTableHeaderRow($thRow);
        }
        if ($strHead != "") {
            $strHead = self::thead($strHead);
        }
        
        foreach ($body as $tr) {
            $strBody .= self::renderTableRow($tr);
        }
        if ($strBody != "") {
            $strBody = self::tbody($strBody);
        }
        
        return self::tag("table", $strHead.$strBody, $attributes);
    }
668519
public function getStrippedText($_stripBasic = false, $_replacements = array())
    {
        $text = $this->text;

        if ($_stripBasic) {
            $text = strip_tags($text);
        }

        $patterns = "/{[^}]+}/";
        $repl = "";
        $text = preg_replace($patterns, $repl, $text);

        if (count($_replacements) > 0) {
            $text = str_replace(array_keys($_replacements), array_values($_replacements), $text);
            $text = iconv('UTF-8', 'UTF-8//IGNORE', $text);
        }

        return $text;
    }