• notice
  • Congratulations on the launch of the Sought Tech site

Convert sitemap.xml file to urllist.txt file using PHP

If you create a script that can generate a sitemap.xml file, it doesn't make sense to adapt the script to create a urllist.txt file. The best solution is to use this sitemap.xml file to create urllist.txt. The script below will do exactly that.

$lines = file( 'sitemap.xml' );
$allMatches = array();
 
foreach ( $lines as $line_number => $line ) {
  $line = trim($line);
  preg_match_all( '/(?<=\<loc\>)(.*?)(?=\<\/loc\>)/U' , $line, $matches,PREG_SET_ORDER);
   if ($matches){
    if ( $matches[ 0 ][ 0 ] != '' ) {
    $allMatches[] = $matches[ 0 ][ 0 ];
   };
  };
};
 
$list = '' ;
  foreach ( $allMatches as $url ) {
  $list .= $url. "\n" ;
};
$fh = fopen( 'urllist.txt' , "w+" );
fwrite($fh, $list);
fclose($fh);
 
// Print the list to provide some feedback...
echo $list;

The script works by first loading the file into an array with sitemap.xml using the file() function. The script then goes through all the items in the array and picks outlabel and put it into an array. Then, add them to a file called urllist.txt and also print the output to give some indication that the script has run. You can remove it if you want to incorporate it into a larger script.

Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

cheap atorvastatin 80mg & lt;a href="https://lipiws.top/"& gt;atorvastatin where to buy& lt;/a& gt; purchase lipitor online

Uqredq

2024-03-10

Leave a Reply

+