Created
October 12, 2016 20:31
-
-
Save alanmbarr/0afa474835a29b057a64f13a2b76b758 to your computer and use it in GitHub Desktop.
Generate the sitemap for my website
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
#!/usr/bin/env php | |
<? | |
function getDirContents($dir, &$results = array()){ | |
$files = scandir($dir); | |
foreach($files as $key => $value){ | |
if($value == ".git"){ | |
continue; | |
} | |
$path = realpath($dir.DIRECTORY_SEPARATOR.$value); | |
if(!is_dir($path)) { | |
$results[] = $path; | |
} else if($value != "." && $value != "..") { | |
getDirContents($path, $results); | |
$results[] = $path; | |
} | |
} | |
return $results; | |
} | |
$dir = "/home/public/"; | |
$list = getDirContents($dir); | |
$htmlOnly = array_filter($list, function($value){ | |
if(strpos($value,".html") !== FALSE){ | |
return TRUE; | |
} | |
}); | |
$htmlOnly = array_map(function($value){ return str_replace("/home/public/","http://www.alanmbarr.com/", htmlspecialchars($value) );}, $htmlOnly); | |
echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; | |
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'.PHP_EOL; | |
foreach($htmlOnly as $url){ | |
$xml = <<<XML | |
<url> | |
<loc>$url</loc> | |
</url> | |
XML; | |
echo $xml; | |
echo PHP_EOL; | |
} | |
echo '</urlset>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment