Last active
August 29, 2015 14:20
-
-
Save oomlaut/9c1c33897a6d3e372b65 to your computer and use it in GitHub Desktop.
Generate sitemap.xml formatted file from text file containing list of links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
print("\n"); | |
if(!isset($argv[1])) | |
{ | |
die("! Required argument: filename \n"); | |
} | |
$filename = $argv[1]; | |
print("> Parsing $filename\n"); | |
if(!file_exists($filename)) | |
{ | |
die("! $filename does not exist \n"); | |
} | |
$list = file($filename); | |
print('> Contains ' . count($list) . " entries\n"); | |
$prefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n"; | |
$postfix = "\n</urlset>\n"; | |
// Format a date string to use in the lastmodified node text | |
// $today = date('Y-m-d'); | |
$urlset = array(); | |
foreach ($list as &$url) | |
{ | |
$node = "<url>\n"; | |
$node .= " <loc>" . trim(preg_replace('/\s+/', ' ', $url)) . "</loc>\n"; | |
// $node .= " <lastmod>" . $today . "</lastmod>\n"; // TODO: accept `lastmod` as option: boolean | |
// $node .= " <changefreq>monthly</changefreq>\n"; // TODO: accept `changefreq` as option: string | |
$node .= "</url>"; | |
array_push($urlset, $node); | |
} | |
$output = preg_replace("/([a-z0-9-_]+)\.[a-z]+/i", "$1.xml", $filename); | |
print("> Write to: $output \n"); | |
file_put_contents($output, $prefix . implode("\n", $urlset) . $postfix); | |
exit("\n> Process completed.\n"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://google.com | |
http://bing.com | |
http://yahoo.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment