You are here

function oa_comment_filter_body in OA Comment 7.2

Filter comment body for things like signature and blank lines

Install text filters on comment body field if you want this stripped on save, but here we just strip on display

Parameters

$text:

Return value

string

1 call to oa_comment_filter_body()
oa_comment_preprocess_comment in ./oa_comment.theme.inc
Implements hook_preprocess_comment().

File

./oa_comment.theme.inc, line 100
Preprocess functions for comments.

Code

function oa_comment_filter_body($text) {

  // Remove signatures
  $strip_body_pattern = variable_get('oa_comment_strip_sig', '-- ');
  if (!empty($strip_body_pattern)) {
    $sig_index = strpos($text, $strip_body_pattern . "\n");
    if ($sig_index !== FALSE) {
      $text = drupal_substr($text, 0, $sig_index);
    }
    $sig_index = strpos($text, $strip_body_pattern . "<br>");
    if ($sig_index !== FALSE) {
      $text = drupal_substr($text, 0, $sig_index);
    }
  }

  // Remove > at beginning of replies
  $lines = explode("\n", $text);
  foreach ($lines as $i => $line) {
    $lines[$i] = ltrim($line, '> ');
  }
  $text = implode("\n", $lines);

  // add any needed closing tags.
  $text = _filter_htmlcorrector($text);

  // Convert line breaks and ensure text has <p> tags
  $text = _filter_autop($text);

  // add any needed closing tags.
  $text = _filter_htmlcorrector($text);

  // Remove leading and trailing <br> tags
  $text = preg_replace('/^\\s*(?:<br\\s*\\/?\\s*>)+|(?:<br\\s*\\/?\\s*>)+\\s*$/i', '', $text);
  $text = preg_replace('/(?:<br\\s*\\/?\\s*>)+\\s*<\\/div>/i', '</div>', $text);
  $text = preg_replace('/(?:<br\\s*\\/?\\s*>)+\\s*<\\/p>/i', '</p>', $text);

  // Remove empty spans
  $text = preg_replace('/<span[^>]*>(&nbsp;|\\s)*<\\/span>/ui', '', $text);

  // Remove empty paragraphs
  $text = preg_replace('/<p[^>]*>(&nbsp;|\\s|<br[^>]*>)*<\\/p>/ui', '', $text);
  return $text;
}