You are here

function XBBCodeFilter::filter_tags in Extensible BBCode 6

Same name and namespace in other branches
  1. 5 xbbcode-filter.php \XBBCodeFilter::filter_tags()
1 call to XBBCodeFilter::filter_tags()
XBBCodeFilter::process in ./xbbcode.filter.inc

File

./xbbcode.filter.inc, line 103

Class

XBBCodeFilter

Code

function filter_tags($text, $pairs) {
  if ($pairs) {
    foreach ($pairs as $id => $name) {

      // for all pairs...
      $pattern = '/\\[' . $name . '(=([^\\]]*))?-' . $id . '-\\](.*)\\[\\/' . $name . '-' . $id . '-\\]/ims';
      if ($this->tags[$name]['multiarg']) {

        // set the multi-arg pattern
        $pattern = str_replace("=", "[= ]", $pattern);
      }
      if ($this->tags[$name]['dynamic'] || $this->tags[$name]['multiarg']) {

        // multi-arg and dynamics are handled by an eval, and this is it.
        $pattern .= 'e';
        $replace = '$this->generate_tag("' . $name . '",\'$1\',\'$3\')';
      }
      else {

        /* we could get rid of the pairs array entirely if we didn't need the dynamic/static
         * state. But since most tags are static, making the distinction saves performance. */
        $replace = str_replace(array(
          '{content}',
          '{option}',
        ), array(
          '$3',
          '$2',
        ), $this->tags[$name]['replacewith']);
      }
      $text = preg_replace($pattern, $replace, $text);
    }
  }

  /* now clean up the dangling opening tags */
  $text = preg_replace('/\\[([^\\]]+)-[0-9]+-\\]/i', '[$1]', $text);
  return $text;
}