You are here

class Messaging_Text in Messaging 7

Message text class.

A Text is a container for renderable elements. Renderable elements can be:

  • An array for drupal_render()
  • A simple string
  • Other Text objects (Texts can be nested)

Hierarchy

Expanded class hierarchy of Messaging_Text

File

messaging_template/messaging_template.inc, line 15
Drupal Messaging Framework - Text filtering functions

View source
class Messaging_Text {

  /**
   * Converts strings to plain utf-8 single line
   */
  static function check_subject($text) {
    $text = check_plain($text);

    // taken from _sanitizeHeaders() in PEAR mail() : http://pear.php.net/package/Mail
    $text = preg_replace('=((0x0A/%0A|0x0D/%0D|\\n|\\r)\\S).*=i', NULL, $text);
    return $text;
  }

  /**
   * Clean text of HTML stuff and optionally of line endings
   *
   * @param $text
   *   Dirty HTML text to be filtered
   * @param $newline
   *   Optional string to be used as line ending
   */
  static function text_clean($text, $newline = NULL) {

    // HTML entities to plain text conversion.
    $text = decode_entities($text);

    // Filters out all remaining HTML tags
    $text = filter_xss($text, array());

    // Optionally, replace new lines
    if (!is_null($newline)) {
      $text = str_replace("\n", $newline, $text);
    }

    // Trim out remaining beginning/ending spaces
    $text = trim($text);
    return $text;
  }

  /**
   * Truncate messages to given length.  Adapted from node_teaser() in node.module
   */
  static function text_truncate($text, $length) {

    // If we have a short message, return the message
    if (drupal_strlen($text) < $length) {
      return $text;
    }

    // Initial slice.
    $teaser = truncate_utf8($text, $length);
    $position = 0;

    // Cache the reverse of the message.
    $reversed = strrev($teaser);

    // split at paragraph boundaries.
    $breakpoints = array(
      '</p>' => 0,
      '<br />' => 6,
      '<br>' => 4,
      "\n" => 1,
    );

    // We use strpos on the reversed needle and haystack for speed.
    foreach ($breakpoints as $point => $offset) {
      $length = strpos($reversed, strrev($point));
      if ($length !== FALSE) {
        $position = -$length - $offset;
        return $position == 0 ? $teaser : substr($teaser, 0, $position);
      }
    }

    // When even the first paragraph is too long, we try to split at the end of
    // the last full sentence.
    $breakpoints = array(
      '. ' => 1,
      '! ' => 1,
      '? ' => 1,
      ' ' => 0,
    );
    $min_length = strlen($reversed);
    foreach ($breakpoints as $point => $offset) {
      $length = strpos($reversed, strrev($point));
      if ($length !== FALSE) {
        $min_length = min($length, $min_length);
        $position = 0 - $length - $offset;
      }
    }
    return $position == 0 ? $teaser : substr($teaser, 0, $position);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Messaging_Text::check_subject static function Converts strings to plain utf-8 single line
Messaging_Text::text_clean static function Clean text of HTML stuff and optionally of line endings
Messaging_Text::text_truncate static function Truncate messages to given length. Adapted from node_teaser() in node.module