Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.31 KB | None | 0 0
  1.  
  2.     public function collectResourcesFromHtml($html) {
  3.         preg_match_all(self::MATCH_SRC, $html, $matches);
  4.         $result = array_filter(array_merge($matches[1], $matches[2], $matches[3], $matches[4]));
  5.  
  6.         if (count($result) >= 3) { // min 3 resources found
  7.             foreach ($result as $resource) {
  8.                 $this->generateLinkHeader($resource);
  9.             }
  10.  
  11.             header('Link: ' . implode(',', $this->links));
  12.         }
  13.     }
  14.  
  15.  
  16.     public function generateLinkHeader($resource) {
  17.         $resource = ltrim($resource, '.');
  18.  
  19.         if ($resource[0] != '/') {
  20.             $resource = '/'.$resource;
  21.         }
  22.  
  23.         $link = "<".$resource.">; rel=preload";
  24.  
  25.         $extension = substr($resource, strrpos($resource, '.')+1);
  26.  
  27.  
  28.         if (strrpos($extension, '?')) {
  29.             $extension = substr($extension, 0,strrpos($extension, '?'));
  30.         }
  31.  
  32.         switch ($extension) {
  33.             case 'css':
  34.                 $link.= '; as=style';
  35.                 break;
  36.             case 'js':
  37.                 $link.= '; as=script';
  38.                 break;
  39.             case 'png':
  40.             case 'jpg':
  41.                 $link.= '; as=image';
  42.                 break;
  43.             default:
  44.                 $link.= '';
  45.         }
  46.  
  47.         $this->links[] = $link;
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement