You are here

function _glossify_replace_terms_phpdom in Glossify 6.3

Uses PHP's built in DOM extension if available. Adapted from http://stackoverflow.com/questions/3151064/find-and-replace-keywords-by-...

Parameters

$text:

$configurations:

$node:

Return value

text

1 call to _glossify_replace_terms_phpdom()
glossify_filter in ./glossify.module
Implementation of hook_filter().

File

./glossify.module, line 461

Code

function _glossify_replace_terms_phpdom($text, $configurations, $node) {
  $original = $text;
  $text = '<meta http-equiv="content-type" content="text/html; charset=utf-8">' . $text;
  $dom = new DOMDocument();
  $dom->formatOutput = true;
  @$dom
    ->loadHTML($text);
  if (!$dom
    ->hasChildNodes()) {
    return $original;
  }
  $xpath = new DOMXpath($dom);
  $xpath_query = "//text()";

  // If there are no text nodes, there is nothing to do.
  $text_nodes = $xpath
    ->query($xpath_query);
  if (0 == $text_nodes->length) {
    return $original;
  }
  $has_replacements = false;
  foreach ($configurations as $config_name => $configuration) {
    if (isset($configuration['from'][$node->type])) {
      $enabled_styles = array_filter($configuration['style']);

      // Check for empty exclusion list.
      $exclude_tags = is_array($configuration['excl_tags']) ? $configuration['excl_tags'] : array();
      $keywords = _fetch_possible_keywords($config_name, $configuration, $node->nid);
      foreach ($keywords as $kwd) {
        list($term_title, $target_url) = $kwd;
        if (empty($term_title)) {
          continue;
        }
        $rx = _glossify_regex($configuration, $term_title);
        if (!preg_match($rx, $original)) {
          continue;
        }
        foreach ($enabled_styles as $style => $enabled) {
          $replacement = _fetch_replacement($style, $term_title, $target_url);
          $child = $dom->firstChild;
          while ($child) {
            if (_glossify_process_domnode($child, $dom, $rx, $replacement, $configuration['excl_mode'], $exclude_tags, $configuration['only_first'])) {
              $has_replacements = true;
              if ($configuration['only_first']) {
                break;
              }
            }
            $child = $child->nextSibling;
          }
        }
      }
    }
  }
  if (false == $has_replacements) {
    return $original;
  }

  // Return just what is in the body tags of our document.
  $body = $dom
    ->GetElementsByTagName('body')
    ->item(0);
  if ($body instanceof DOMNode) {

    // PHP 5.2 compatibility, saveHTML does not accept args.
    $return = new DOMDocument();
    $root = $return
      ->createElement('body');
    $root = $return
      ->appendChild($root);
    $result_node = $return
      ->importNode($body, true);
    $return->documentElement
      ->appendChild($result_node);
    $out = $return
      ->saveHTML();
    $out = str_replace(array(
      '<body>',
      '</body>',
    ), '', $out);
    return $out;
  }
  else {
    return $original;
  }
}