You are here

function smiley_process_text in Smiley 7

Replaces smile codes in the text by corresponded images.

3 calls to smiley_process_text()
SmileyFilterWebTestCase::CheckSmileysWereReplaced in ./smiley.test
Checks the $original_text converting to a $expected_text correctly.
SmileyFilterWebTestCase::testImageSrc in ./smiley.test
Make sure the <img> element contains the right src value.
smiley_filter_process in ./smiley.module
Smileys filter process callback.

File

./smiley.module, line 110
Implements the necessary hooks.

Code

function smiley_process_text($text, $smileys) {
  global $base_path;
  $output = '';
  $ignore = 0;
  $packs_path = variable_get('smiley_path', drupal_get_path('module', 'smiley') . '/packs');
  $css_class = check_plain(variable_get('smiley_css_class', ''));
  if (!empty($css_class)) {
    $css_class = 'class="' . $css_class . '" ';
  }
  $chunks = preg_split('#(</?(?:code|pre)[^>]*>)#i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
  foreach ($chunks as $chunk) {
    if (preg_match('#(</?)(code|pre)[^>]*(>)#i', $chunk, $matches)) {
      $tag = $matches[1] . $matches[2] . $matches[3];
      if ($tag == '<code>' || $tag == '<pre>') {
        $ignore++;
      }
      elseif ($tag == '</code>' || $tag == '</pre>') {
        $ignore--;
      }
    }
    elseif ($chunk && !$ignore) {
      foreach ($smileys as $smiley) {
        $image = '<img src="' . file_create_url('smiley://' . $smiley->uri) . '" alt="' . $smiley->description . '" ' . $css_class . '/>';
        $acronyms = explode(' ', $smiley->acronyms);
        foreach ($acronyms as $acronym) {
          if (strlen($acronym) == 0) {
            continue;
          }
          $chunk = preg_replace('~(?<=^|>|\\s|&nbsp;)(' . preg_quote($acronym, '~') . ')(?=$|</|<area\\s*/?>|<base\\s*/?>|<br\\s*/?>|<col\\s*/?>|<command\\s*/?>|<embed\\s*/?>|<hr\\s*/?>|<img\\s*/?>|<input\\s*/?>|<link\\s*/?>|<meta\\s*/?>|<param\\s*/?>|<source\\s*/?>|\\s|&nbsp;)~U', $image, $chunk);
        }
      }
    }
    $output .= $chunk;
  }
  return $output;
}