You are here

function _customfilter_process in Custom filter 6.2

Same name and namespace in other branches
  1. 5 customfilter.module \_customfilter_process()
  2. 6 customfilter.module \_customfilter_process()
  3. 7.2 customfilter.module \_customfilter_process()
  4. 7 customfilter.module \_customfilter_process()
1 string reference to '_customfilter_process'
customfilter_filter in ./customfilter.module
Implements hook_filter().

File

./customfilter.module, line 344
Allow users 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 = drupal_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) {
    $code =& Vars::staticValue('_customfilter_replace_callback');
    $code = $replacement;
    $result = preg_replace_callback($pattern, '_customfilter_replace_callback', $result);
  }
  else {
    $result = preg_replace($pattern, $replacement, $result);
  }
  return $result;
}