Skip to content

Instantly share code, notes, and snippets.

@alanmbarr
Created October 12, 2016 20:31
Show Gist options
  • Save alanmbarr/0afa474835a29b057a64f13a2b76b758 to your computer and use it in GitHub Desktop.
Save alanmbarr/0afa474835a29b057a64f13a2b76b758 to your computer and use it in GitHub Desktop.
Generate the sitemap for my website
#!/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