php自動生成sitemap地圖的代碼大綱
如何生成sitemap地圖呢?本文分享一例php代碼,用於自動動態生成最新的`sitemap地圖文件,並通知google網站地圖的更新,感興趣的朋友參考下吧。
內容:
php自動生成sitemap地圖
例子,:主要生成sitemap的類。
代碼:
複製代碼 代碼示例:
<?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=”″
xmlns:xsi=””
xsi:schemaLocation=”
”>’;
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();
}
// 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};)/”,”&” ,
strtr($str, $translation));
}
}
?>
:調用,具體實現sitemap。
複製代碼 代碼示例:
<?php
// redirect requests to dynamic to their keyword rich versions
require_once ‘/’;
define(‘SITE_DOMAIN’, ‘’);
// create the Sitemap object
$s = new Sitemap();
// add sitemap items
$s->addItem(SITE_DOMAIN);
$s->addItem(SITE_DOMAIN.”/”);
$s->addItem(SITE_DOMAIN.”/”);
…
//連接數據庫,生成URL並通過條用$s->addItem()加入到sitemap中。
// output sitemap
if (isset($_GET['target']))
{
// generate Google sitemap
if (($target = $_GET['target']) == ‘google’)
{
echo $s->getGoogle();
}
}
?>
-
PHP考試筆試題
用寶珠打扮自己,不如用知識充實自己。以下是小編為大家搜索整理的PHP考試筆試題,希望能給大家帶來幫助!更多精彩內容請及時關注我們應屆畢業生考試網!1、{PHP題目}標識符是變量的名稱。PHP中的標識符用$+變量名來表示。標識符在PHP中遵循下列選項中的那些規則?()...
-
php如何基於dom實現圖書xml格式數據
導語:php如何基於dom實現圖書xml格式數據呢?下面是小編給大家提供的代碼實現方法,大家可以參考閲讀,更多詳情請關注應屆畢業生考試網。<?php$doc=newDOMDocument();$doc->load('');$books=$doc->getElementsByTagName("book");foreach($booksas$book){$aut...
-
PHP與UTF-8之間的最佳實踐
《PHP中的字符串、編碼、UTF-8》一文中描述了一些列的基礎知識,比較枯燥,現在來説點有用的——PHP字符串處理的最佳實踐,本文是PHP、字符串、編碼、UTF-8相關知識的第二部分。先説結論——在PHP中的各個方面使用UTF-8編碼。希望對大家學習PHP...
-
PHP入門學習方法
每個人的學習方式不同,寫這篇文章的目的是分享一下自己的學習過程,僅供參考,不要一味的用別人的學習方法,找對自己有用的學習方式,關於php語言怎麼學呢?下面是相關的知識,歡迎閲讀。其實學習一門新語言並不是太難,重要的是你有沒有準備好去學好它,時間的長短和個人的能...