You are here

function smiley_filter_process in Smiley 6

Same name and namespace in other branches
  1. 7 smiley.module \smiley_filter_process()
1 call to smiley_filter_process()
smiley_filter in ./smiley.module
Implementation of hook_filter().

File

./smiley.module, line 250

Code

function smiley_filter_process($text) {
  $text = ' ' . $text . ' ';
  $list = _smiley_list(0, ' ORDER BY weight');

  // Don't process for <code> and <pre> elements.
  $chunks = preg_split('@(</?(?:code|pre)[^>]*>)@i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
  $ignore = FALSE;
  $ignoretag = '';
  $output = '';
  foreach ($chunks as $i => $chunk) {
    if ($i % 2) {

      // Opening or closing tag?
      $open = $chunk[1] != '/';
      list($tag) = split('[ >]', substr($chunk, 2 - $open), 2);
      if (!$ignore) {
        if ($open) {
          $ignore = TRUE;
          $ignoretag = $tag;
        }
      }
      else {
        if (!$open && $ignoretag == $tag) {
          $ignore = FALSE;
          $ignoretag = '';
        }
      }
    }
    else {
      if (!$ignore) {
        foreach ($list as $smiley) {
          $acronyms = explode(" ", $smiley->acronyms);
          $alt = str_replace('\\', '\\\\', check_plain($smiley->description));
          foreach ($acronyms as $a) {
            if ($smiley->standalone) {
              $chunk = eregi_replace("([ ,\\.\\?!:\\(\\)\r\n\\<\\>])" . preg_quote($a) . "([ ,\\.\\?!:\\(\\)\r\n\\<\\>])", "\\1<img src=\"" . check_url($GLOBALS['base_url'] . '/' . $smiley->image) . "\" title=\"" . check_plain($alt) . "\" alt=\"" . check_plain($alt) . "\" class=\"smiley-content\"/>\\2", $chunk);
            }
            else {
              $chunk = eregi_replace(preg_quote($a), '<img src="' . check_url($GLOBALS['base_url'] . '/' . $smiley->image) . '" title="' . check_plain($alt) . '" alt="' . check_plain($alt) . '" />', $chunk);
            }
          }
        }
      }
    }
    $output .= $chunk;
  }
  return $output;
}