/**
  * Inserts tag links into an HTML-formatted text.
  *
  * @param string $html
  * @param array $tags
  * @param array $options
  * @return string
  */
 public static function autoTag($html, array $tags, array &$options = array())
 {
     if (empty($tags)) {
         return $html;
     }
     $html = strval($html);
     $htmlNullified = utf8_strtolower($html);
     $htmlNullified = preg_replace_callback('#<a[^>]+>.+?</a>#', array(__CLASS__, '_autoTag_nullifyHtmlCallback'), $htmlNullified);
     $htmlNullified = preg_replace_callback('#<[^>]+>#', array(__CLASS__, '_autoTag_nullifyHtmlCallback'), $htmlNullified);
     // prepare the options
     $onceOnly = empty($options['onceOnly']) ? false : true;
     $options['autoTagged'] = array();
     // reset this
     // sort tags with the longest one first
     // since 1.0.3
     usort($tags, array(__CLASS__, '_autoTag_sortTagsByLength'));
     foreach ($tags as $tag) {
         $offset = 0;
         $tagText = utf8_strtolower($tag['tag']);
         $tagLength = utf8_strlen($tagText);
         while (true) {
             $pos = utf8_strpos($htmlNullified, $tagText, $offset);
             if ($pos !== false) {
                 // the tag has been found
                 if (self::_autoTag_hasValidCharacterAround($html, $pos, $tagText)) {
                     // and it has good surrounding characters
                     // start replacing
                     $displayText = utf8_substr($html, $pos, $tagLength);
                     $template = new XenForo_Template_Public('tinhte_xentag_bb_code_tag_tag');
                     $template->setParam('tag', $tag);
                     $template->setParam('displayText', $displayText);
                     $replacement = $template->render();
                     if (strlen($replacement) === 0) {
                         // in case template system hasn't been initialized
                         $replacement = sprintf('<a href="%s">%s</a>', XenForo_Link::buildPublicLink('tags', $tag), $displayText);
                     }
                     $html = utf8_substr_replace($html, $replacement, $pos, $tagLength);
                     $htmlNullified = utf8_substr_replace($htmlNullified, str_repeat('_', utf8_strlen($replacement)), $pos, $tagLength);
                     // sondh@2012-09-20
                     // keep track of the auto tagged tags
                     $options['autoTagged'][$tagText][$pos] = $replacement;
                     $offset = $pos + utf8_strlen($replacement);
                     if ($onceOnly) {
                         // auto link only once per tag
                         // break the loop now
                         break;
                         // while (true)
                     }
                 } else {
                     $offset = $pos + $tagLength;
                 }
             } else {
                 // no match has been found, stop working with this tag
                 break;
                 // while (true)
             }
         }
     }
     return $html;
 }
Beispiel #2
0
 /**
  * Inserts tag links into an HTML-formatted text.
  *
  * @param string $html
  * @param array $tags
  * @param array $options
  */
 public static function autoTag($html, array $tagsOrTexts, array &$options = array())
 {
     if (empty($tagsOrTexts)) {
         return $html;
     }
     $html = strval($html);
     $tagTexts = Tinhte_XenTag_Helper::getTextsFromTagsOrTexts($tagsOrTexts);
     // prepare the options
     $onceOnly = empty($options['onceOnly']) ? false : true;
     $options['autoTagged'] = array();
     // reset this
     // sort tags with the longest one first
     // since 1.0.3
     usort($tagTexts, array(__CLASS__, '_autoTag_sortTagsByLength'));
     foreach ($tagTexts as $tagText) {
         $offset = 0;
         $tagLength = utf8_strlen($tagText);
         while (true) {
             $pos = Tinhte_XenTag_Helper::utf8_stripos($html, $tagText, $offset);
             if ($pos !== false) {
                 // the tag has been found
                 if (!self::_autoTag_isBetweenHtmlTags($html, $pos) and self::_autoTag_hasValidCharacterAround($html, $pos, $tagText)) {
                     // and it's not between HTML tags,
                     // with good surrounding characters
                     // start replacing
                     $template = new XenForo_Template_Public('tinhte_xentag_bb_code_tag_tag');
                     $template->setParam('tag', $tagText);
                     $template->setParam('displayText', utf8_substr($html, $pos, $tagLength));
                     $replacement = $template->render();
                     $html = utf8_substr_replace($html, $replacement, $pos, $tagLength);
                     // sondh@2012-09-20
                     // keep track of the auto tagged tags
                     $options['autoTagged'][$tagText][$pos] = $replacement;
                     $offset = $pos + utf8_strlen($replacement);
                     if ($onceOnly) {
                         // auto link only once per tag
                         // break the loop now
                         break;
                         // while (true)
                     }
                 } else {
                     $offset = $pos + $tagLength;
                 }
             } else {
                 // no match has been found, stop working with this tag
                 break;
                 // while (true)
             }
         }
     }
     return $html;
 }