Guest User

Sitemap VS reader

a guest
Apr 12th, 2020
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.19 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Either edit the $sites array, or make a sites.txt file in the same directory and
  5.  * comment out the $sites array and uncomment the line below
  6.  */
  7.  
  8. $sites = array(
  9.     'https://www.androidcentral.com/',
  10.     'https://handsetexpert.com/'
  11. );
  12.  
  13. // $sites = explode("\n", file_get_contents(__DIR__ . '/sites.txt'));
  14.  
  15. $urls = [];
  16. foreach ($sites as $site) {
  17.     $urls = array_merge($urls, fetchUrls(rtrim($site, '/') . '/sitemap.xml'));
  18. }
  19.  
  20. $counts = [
  21.     'match' => 0,
  22.     'no_match' => 0,
  23.     'perc' => 0
  24. ];
  25.  
  26. foreach ($urls as $url) {
  27.     if (preg_match('/(.*?)(\-|_)vs(\-|_)(.*?)/i', $url) == true) {
  28.         $counts['match']++;
  29.     } else {
  30.         $counts['no_match']++;
  31.     }
  32. }
  33.  
  34. $counts['perc'] = round(($counts['match'] / ($counts['match'] + $counts['no_match'])) * 100, 2) . '%';
  35.  
  36. print_r($counts);
  37.  
  38. function fetchUrls(string $sitemapUrl)
  39. {
  40.     $content = file_get_contents($sitemapUrl);
  41.  
  42.     preg_match_all('/<loc>(.*?)<\/loc>/', $content, $urls);
  43.  
  44.     $final = $urls[1] ?? [];
  45.  
  46.     foreach ($final as $url) {
  47.         if (stripos($url, 'sitemap') !== false) {
  48.             $final = array_merge($final, fetchUrls($url));
  49.         }
  50.     }
  51.  
  52.     return $final;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment