Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kadko/0860836a2bdd3a59c011eb2ddb0e12d7 to your computer and use it in GitHub Desktop.
Save kadko/0860836a2bdd3a59c011eb2ddb0e12d7 to your computer and use it in GitHub Desktop.
<?php
/* Big File Splitter v0.1
Kadir Korkmaz
*/
$fileStr = file_get_contents('sitemap.xml');
$past = 0;
$PIN = '</url>';//ENTER sitemap repetitive item element closing tag
$PINlen = mb_strlen( $PIN );
$partSize = 2*1024*1024;//BYTES, 20MB
$len = mb_strlen($fileStr) / $partSize;
echo 'Total File: ' . floor($len);
echo '<br>';
echo 'Each File Size: ' . $partSize;
echo '<br>';
echo 'Each File Size(MB): ' . $partSize/(1024*1024);
for($i=1; $i < $len; $i++){
$splitpos = mb_strpos($fileStr, $PIN, $i*$partSize);
if( $i == floor($len) ){//if last part
$part = mb_substr($fileStr, $past + $PINlen );
}else{
if($i != 1){ $marginS = $PINlen; $marginE = 0; }else{ $marginS = 0; $marginE = $PINlen; }//first part without margin
$part = mb_substr($fileStr, $past + $marginS, $splitpos - $past + $marginE);
}
$past = $splitpos;//get previous last as current start offset
$part = str_replace('&nbsp;',' ', $part);
$start = '<!--?xml version="1.0" encoding="UTF-8"?--><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">';
$end = '</urlset>';
if( $i == 1 ){ $start = ''; }
if( $i==floor($len) ){ $end = ''; }
file_put_contents('sitemap'. $i .'.xml', $start. $part. $end );
$sitemapsItem .='<sitemap> <loc>https://www.YOURSITE.com/catalog/sitemaps/sitemap'.$i.'.xml</loc><lastmod>2018-11-01T18:23:17+00:00</lastmod> </sitemap>';
}
$sitemaps = '<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
'. $sitemapsItem .'
</sitemapindex>';
file_put_contents('sitemapindex.xml', $sitemaps);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment