Frontend\Core\Engine\Model::convertToPlainText PHP Метод

convertToPlainText() публичный статический Метод

Get plain text for a given text
public static convertToPlainText ( string $text, boolean $includeAHrefs = true, boolean $includeImgAlts = true ) : string
$text string The text to convert.
$includeAHrefs boolean Should the url be appended after the link-text?
$includeImgAlts boolean Should the alt tag be inserted for images?
Результат string
    public static function convertToPlainText($text, $includeAHrefs = true, $includeImgAlts = true)
    {
        // remove tabs, line feeds and carriage returns
        $text = str_replace(array("\t", "\n", "\r"), '', $text);
        // remove the head-, style- and script-tags and all their contents
        $text = preg_replace('|\\<head[^>]*\\>(.*\\n*)\\</head\\>|isU', '', $text);
        $text = preg_replace('|\\<style[^>]*\\>(.*\\n*)\\</style\\>|isU', '', $text);
        $text = preg_replace('|\\<script[^>]*\\>(.*\\n*)\\</script\\>|isU', '', $text);
        // put back some new lines where needed
        $text = preg_replace('#(\\<(h1|h2|h3|h4|h5|h6|p|ul|ol)[^\\>]*\\>.*\\</(h1|h2|h3|h4|h5|h6|p|ul|ol)\\>)#isU', "\n\$1", $text);
        // replace br tags with newlines
        $text = preg_replace('#(\\<br[^\\>]*\\>)#isU', "\n", $text);
        // replace links with the inner html of the link with the url between ()
        // eg.: <a href="http://site.domain.com">My site</a> => My site (http://site.domain.com)
        if ($includeAHrefs) {
            $text = preg_replace('|<a.*href="(.*)".*>(.*)</a>|isU', '$2 ($1)', $text);
        }
        // replace images with their alternative content
        // eg. <img src="path/to/the/image.jpg" alt="My image" /> => My image
        if ($includeImgAlts) {
            $text = preg_replace('|\\<img[^>]*alt="(.*)".*/\\>|isU', '$1', $text);
        }
        // decode html entities
        $text = html_entity_decode($text, ENT_QUOTES, 'ISO-8859-15');
        // remove space characters at the beginning and end of each line and clear lines with nothing but spaces
        $text = preg_replace('/^\\s*|\\s*$|^\\s*$/m', '', $text);
        // strip tags
        $text = strip_tags($text, '<h1><h2><h3><h4><h5><h6><p><li>');
        // format heading, paragraphs and list items
        $text = preg_replace('|\\<h[123456]([^\\>]*)\\>(.*)\\</h[123456]\\>|isU', "\n** \$2 **\n", $text);
        $text = preg_replace('|\\<p([^\\>]*)\\>(.*)\\</p\\>|isU', "\$2\n", $text);
        $text = preg_replace('|\\<li([^\\>]*)\\>\\n*(.*)\\n*\\</li\\>|isU', "- \$2\n", $text);
        // replace 3 and more line breaks in a row by 2 line breaks
        $text = preg_replace('/\\n{3,}/', "\n\n", $text);
        // use php constant for new lines
        $text = str_replace("\n", PHP_EOL, $text);
        // trim line breaks at the beginning and ending of the text
        $text = trim($text, PHP_EOL);
        // return the plain text
        return $text;
    }

Usage Example

Пример #1
0
 /**
  * Default constructor.
  *
  * @param string $title       The title for the item.
  * @param string $link        The link for the item.
  * @param string $description The content for the item.
  */
 public function __construct($title, $link, $description)
 {
     // set UTM-campaign
     $this->utm['utm_campaign'] = CommonUri::getUrl($title);
     // convert to plain text
     $description = FrontendModel::convertToPlainText($description);
     // set title
     $this->setSummary($title);
     // set url
     $this->setUrl(FrontendModel::addURLParameters($link, $this->utm));
     // set description
     $this->setDescription($this->processLinks($description));
     // set identifier
     $this->setUniqueIdentifier(md5($link));
     // build properties
     $properties['X-GOOGLE-CALENDAR-CONTENT-TITLE'] = $title;
     $properties['X-GOOGLE-CALENDAR-CONTENT-ICON'] = SITE_URL . '/favicon.ico';
     $properties['X-GOOGLE-CALENDAR-CONTENT-URL'] = $this->getUrl();
     // set properties
     $this->setXProperties($properties);
 }