You are here

function _ai_fixup_html_encoding in Content Injector (formerly AdSense Injector) 6.3

Same name and namespace in other branches
  1. 7.3 adsense_injector.module \_ai_fixup_html_encoding()

// From: http://devzone.zend.com/article/8855

Parameters

unknown_type $dom:

File

./adsense_injector.module, line 295
Inject adsense ads into node content automatically.

Code

function _ai_fixup_html_encoding($dom) {
  $xpath = new DOMXPath($dom);
  $metaTags = $xpath
    ->query('/html/head/meta');

  // Unfortunately DOMXPath supports only XPath 1.0 and we have to iterate
  // through meta tags instead of selecting the node using
  // '/html/head/meta[lower-case(@http-equiv)="content-type"]'
  // (fn:lower-case() function came with XPath 2.0)
  for ($count = 0; $count < $metaTags->length; $count++) {
    $httpEquivAttribute = $metaTags
      ->item($count)->attributes
      ->getNamedItem('http-equiv');
    if ($httpEquivAttribute !== null && strtolower($httpEquivAttribute->value) == 'content-type') {
      $fragment = $dom
        ->createDocumentFragment();
      $fragment
        ->appendXML('<meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>');
      $metaTags
        ->item($count)->parentNode
        ->replaceChild($fragment, $metaTags
        ->item($count));
      break;
    }
  }

  // Do nothing if meta tag is not found
  return $dom;
}