topical media & game development

talk show tell print

professional-search-09-seophp-include-sitemap.inc.php / php



  <?php
  
  // sitemap generator class
  class Sitemap
  {
    // constructor receives the list of URLs to include in the sitemap
    function Sitemap(items = array()) 
    {
      this->_items = items;
    }
  
    // add a new sitemap item  
    function addItem(url, 
                     lastmod = '', 
                     changefreq = '', 
                     priority = '', 
                     additional_fields = array())
    {
      this->_items[] = array_merge(array('loc' => url, 
                                          'lastmod' => lastmod, 
                                          'changefreq' => changefreq, 
                                          'priority' => priority), 
                                    additional_fields);
    }
  
    // get Google sitemap  
    function getGoogle()
    {
      ob_start();
      header('Content-type: text/xml');
      echo '<?xml version="1.0" encoding="UTF-8"?>';
      echo '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">';
      foreach (this->_items as i)
      {
        echo '<url>';
        foreach (i as index => _i) 
        {
          if (!_i) continue;
          echo "<index>" . this->_escapeXML(_i) . "</index>";
        }
        echo '</url>';
      }
      echo '</urlset>';
      return ob_get_clean();  
    }  
  
    // get Yahoo sitemap
    function getYahoo()
    {
      ob_start();
      header('Content-type: text/plain');
      foreach (this->_items as i) 
      {
        echo i['loc'] . "\n";
      }
      return ob_get_clean(); 
    } 
  
    // escape string characters for inclusion in XML structure
    function _escapeXML(str) 
    { 
      translation = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);    
      foreach (translation as key => value) 
      {
        translation[key] = '&#' . ord(key) . ';'; 
      }
      translation[chr(38)] = '&';   
      return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/","&#38;" , 
                          strtr(str, translation)); 
    } 
  }
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.