01 <html>
02 <body>
03 <?php
04     $BaseURL = "http://www.(userdomain)/page/";
05     $BasePath = "./";
06     $SitemapDataPath = "sitemap.xml";
07     $SitemapNewDataPath = "sitemap-new.xml";
08 
09     if(!$SitemapDataHandle = fopen($BasePath . $SitemapDataPath, "r")) {
10         die("fopen(ERROR): $BasePath$SitemapDataPath");
11     }
12 
13     $cnt = 0;
14     while ($line = fgets($SitemapDataHandle)) {
15         if(preg_match('/<loc>(.+)<\/loc>/i', $line, $match)) {
16             $SitelinkArray[$cnt]['src'] = $match[1];
17             $cnt++;
18         }
19     }
20     $LinkNum = $cnt;
21 
22     fclose($SitemapDataHandle);
23 
24     printf("<div>found %d links.</div>\n", $LinkNum);
25 
26     $TitleNum = 0;
27     for($cnt = 0; $cnt < $LinkNum; $cnt++) {
28         $SitesrcDataName = str_replace($BaseURL, $BasePath, $SitelinkArray[$cnt]['src']);
29 
30         if(!$SitesrcDataHandle = fopen($SitesrcDataName, "r")) {
31             die("fopen(ERROR): $SitesrcDataName");
32         }
33 
34         $SitelinkArray[$cnt]['date'] = date("Y-m-d\TH:i:sP", filemtime($SitesrcDataName));
35 
36         while ($line = fgets($SitesrcDataHandle)) {
37             if(preg_match('/<title>(.+)<\/title>/i', $line, $match)) {
38                 $SitelinkArray[$cnt]['title'] = $match[1];
39                 $TitleNum++;
40                 break;
41             }
42         }
43 
44         fclose($SitesrcDataHandle);
45     }
46 
47     printf("<div>found %d titles.</div>\n", $TitleNum);
48 
49     if(!$SitemapNewDataHandle = fopen($BasePath . $SitemapNewDataPath, "w")) {
50         die("fopen(ERROR): $BasePath$SitemapNewDataPath");
51     }
52 
53     fputs($SitemapNewDataHandle, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
54     fputs($SitemapNewDataHandle, "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n");
55 
56     for($cnt = 0; $cnt < $LinkNum; $cnt++) {
57         fputs($SitemapNewDataHandle, "   <url>\n");
58         fprintf($SitemapNewDataHandle, "      <loc>%s</loc>\n", $SitelinkArray[$cnt]['src']);
59         fprintf($SitemapNewDataHandle, "      <lastmod>%s</lastmod>\n", $SitelinkArray[$cnt]['date']);
60         fputs($SitemapNewDataHandle, "   </url>\n");
61 
62         printf("<div><a href=\"%s\">%s</a></div>\n", $SitelinkArray[$cnt]['src'], $SitelinkArray[$cnt]['title']);
63     }
64 
65     fputs($SitemapNewDataHandle, "</urlset>\n");
66 
67     fclose($SitemapNewDataHandle);
68 
69     printf("<div>create sitemap [%s].</div>\n", $BasePath . $SitemapNewDataPath);
70 ?>
71 </body>
72 </html>