You are here

function _table_altrow_process in Table Alternate Rows 7

Filter process callback.

1 string reference to '_table_altrow_process'
table_altrow_filter_info in ./table_altrow.module
Implements hook_filter_info().

File

./table_altrow.module, line 25
Insert even and odd classes for tables via input filters to allow for proper zebra-style striping.

Code

function _table_altrow_process($text, $filter) {

  // The actual filtering is performed here. The supplied text should be
  // returned, once any necessary substitutions have taken place.
  // First, we have to parse the variable.
  $matches = array();
  $offset = 0;

  // Find a tbody.
  while (preg_match('!(<tbody ?.*>)!i', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
    $offset = $matches[0][1];
    $count = 1;

    // While the tbody is still open.
    while (preg_match('!(<tr( ?.*)>)|(</tbody>)!i', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {

      // +1 so we don't match the same string.
      $offset = $matches[0][1] + 1;

      // Don't process tr's until we find a tbody.
      if ($matches[0][0] == '</tbody>') {
        break;
      }
      $attributes = substr($matches[2][0], 0, strpos($matches[2][0], '>'));
      $found_class = preg_match('/\\bclass\\b="([\\- a-z0-9]*?)"/u', $attributes, $class_body);
      if ($count % 2 == 0) {
        $class = 'even';
      }
      else {
        $class = 'odd';
      }
      if ($found_class) {
        if (strstr($class_body[1], 'even') === FALSE || strstr($class_body[1], 'odd') === FALSE) {
          $class .= " {$class_body[1]}";
        }
        else {
          $class = $class_body[1];
        }
      }
      $new_tag = '<tr class="' . $class . '"' . $matches[2][0] . '>';
      $text = table_altrow_str_replace_count($matches[0][0], $new_tag, $text, $offset - 1, 1);
      $count++;
    }
  }
  return $text;
}