You are here

function _customfilter_process in Custom filter 7.2

Same name and namespace in other branches
  1. 5 customfilter.module \_customfilter_process()
  2. 6.2 customfilter.module \_customfilter_process()
  3. 6 customfilter.module \_customfilter_process()
  4. 7 customfilter.module \_customfilter_process()

@todo Document this function

Parameters

$matches:

1 string reference to '_customfilter_process'
_customfilter_filter_process in ./customfilter.module
Process the text, apply the filters

File

./customfilter.module, line 504
Allows the users with the right permission to define custom filters.

Code

function _customfilter_process($matches) {
  $globals =& _customfilter_globals();
  $result = $matches[0];
  $rule = end($globals->stack);
  $code = $rule['code'];
  $pattern = $rule['pattern'];
  $replacement = $rule['replacement'];
  if (is_array($sub = $rule['sub']) && count($sub)) {
    foreach ($sub as $subrule) {
      if ($subrule['enabled']) {
        $globals->stack[] = $subrule;
        $substr =& $matches[$subrule['matches']];
        $substr = preg_replace_callback($subrule['pattern'], '_customfilter_process', $substr);
        array_pop($globals->stack);
      }
    }
    if ($code) {
      _customfilter_replace_callback($replacement, TRUE);
      $result = _customfilter_replace_callback($matches);
    }
    else {
      $result = $replacement;
      $rmatches = array();
      $reps = array();
      preg_match_all('/([^\\\\]|^)(\\$([0-9]{1,2}|\\{([0-9]{1,2})\\}))/', $replacement, $rmatches, PREG_OFFSET_CAPTURE);
      foreach ($rmatches[4] as $key => $val) {
        if ($val == '') {
          $index = $rmatches[3][$key][0];
        }
        else {
          $index = $rmatches[4][$key][0];
        }
        $offset = $rmatches[2][$key][1];
        $length = strlen($rmatches[2][$key][0]);
        $reps[] = array(
          'index' => $index,
          'offset' => $offset,
          'length' => $length,
        );
      }
      krsort($reps);
      foreach ($reps as $rep) {
        $result = substr_replace($result, $matches[$rep['index']], $rep['offset'], $rep['length']);
      }
    }
  }
  elseif ($code) {
    _customfilter_replace_callback($replacement, TRUE);
    $result = preg_replace_callback($pattern, '_customfilter_replace_callback', $result);
  }
  else {
    $result = preg_replace($pattern, $replacement, $result);
  }
  return $result;
}