Sitemap

' . $title . '' . "\n"; return $link; } /* Function: folder_strip Purpose: strips the next folder from an array of URLs Arguments: $urls, $strip, $start $urls: the array of links from your sitemap file $strip: the folder to strip including a slash (/sales for example) $start: what link to start on in the $urls array Returns: $urls array */ function folder_strip($urls, $strip, $start) { for($i = $start; $i < count($urls); $i++) // Go through every url { $urls[$i] = str_replace($strip, "", $urls[$i]); // strip the folder name out of it } return $urls; } /* Function: format_folder Purpose: this is a recursive function that takes care of a folder in the sitemap file and creates another unordered list for each folder and all the links in a folder, and keeps going until all links and files in the initial folder have been gone through Arguments: $urls, $strip, $links = array() $urls: the array of links from your sitemap file $start: the array key to start on $links = array(): an optional argument that contains the same urls array but necessary for the recursive functionality to work Returns: the link number that it ended on so that the sitemap can continue at the end of the links */ function format_folder($urls, $start, $links = array()) { include("folders.php"); $pos = strpos($urls[$start], "/") + 1; // The position of the first slash $pos_two = strpos($urls[$start], "/", $pos); // The position of the second slash $folder = substr($urls[$start], $pos, $pos_two-$pos); // The next folder name (i.e., in /os/items/ it would be os $disp_folder = ucwords($folder); // Defines a default folder name value if(array_key_exists($folder, $folders)) { $disp_folder = $folders[$folder]; } // Checks to see if you specified a folder name echo "
  • " . $disp_folder . "
  • "; echo '
  • ' . "\n
      \n"; $i = intval($start); // switching variables, had trouble using the $start, acted like a string while ($folder == @substr($urls[$i], $pos, @strpos($urls[$i], "/", $pos)-$pos)) // keeps in the same base folder (i.e., /os/) { $subpos = strpos($urls[$i], "/", $pos_two + 1); // If there is another sub folder, this detects it if($subpos != false) { $i = format_folder(folder_strip($urls, "/" . $folder, $i), $i, $urls); //Call the function again (recursive) } else { if (count($links) > 0) // If this is recursing, this will be used instead of $urls { echo format_url($links[$i]); } else { echo format_url($urls[$i]); } $i++; } } echo "
    \n
  • "; return $i; } ?> Company: Phoenix Development (http://www.phoenixdev.net) Date: 03/08/2007 Version: 1.0 */ /*************** FUNCTIONS ***************/ function curl_open($url) { $curl_handle = curl_init(); // Where should we get the data? curl_setopt ($curl_handle, CURLOPT_URL, $url); // This says not to dump it directly to the output stream, but instead // have it return as a string. curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1); // the following is optional, but you should consider setting it // anyway. It prevents your page from hanging if the remote site is // down. curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, 1); // Now, YOU make the call. $buffer = curl_exec($curl_handle); // And tell it to shut down (when your done. You can always make more // calls if you want.) curl_close($curl_handle); // This is where i'd probably do some extra checks on what i just got. // Paranoia pays dividends. return $buffer; } if($sitemap) { // Check to make sure the sitemap can be found echo "
      "; // $xml = simplexml_load_file($sitemap); $xml = simplexml_load_string(curl_open($sitemap));// Load the sitemap xml file /* $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, $sitemap); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $xml = curl_exec($ch); curl_close($ch); */ // Set variable types $loc = array(); $lastmod = array(); $changefreq = array(); $priority = array(); for($i = 0; $i < count($xml->url); $i++) // Convert to separate arrays (3 of them aren't currently used but may be used in the future) { $loc[$i] = str_replace($website, "", $xml->url[$i]->loc); $lastmod[$i] = $xml->url[$i]->lastmod; $changefreq[$i] = $xml->url[$i]->changefreq; $priority[$i]= $xml->url[$i]->priority; } array_multisort($priority, SORT_NUMERIC, SORT_DESC, $loc, $lastmod, $changefreq); for($i = 0; $i < count($loc); $i++) // For loop to go through all the URLs { if(substr_count($loc[$i], "/", 1) > 0) // Check to see if it's in a folder { $i = format_folder($loc, $i); // takes care of folders and returns the last link spat out } else { echo format_url($loc[$i]); // formats the link so that the Title looks nice and goes to the proper address } } echo "
    "; } else { // If the sitemap can't be found, you should write some sort of error message and I suggest sending an e-mail to yourself include("error.php"); } ?>

    Back to the Forbes Magazine article on Ignatius Piazza