code: public static function findBestMatching(URL $url, array $sites)
      {
          $vhosts = array();
          foreach ($sites as $site)
              foreach ($site->getVirtualHosts() as $vhost)
                  $vhosts[] = $vhost;
  
          // Remove query and fragments from the URL in use
          $my_url = new URL($url);
          $my_url->set('query', null)->set('fragment', null)->toString();
  
          // Match the visited URL with all vhosts and calcualte their textual similarity
          $best_percentage = 0;
          $best_idx = null;
          foreach ($vhosts as $idx => $vhost)
          {
              $host = $vhost->getHost()->toString();
              similar_text($my_url, $host, $percentage);
              if ($best_idx === null || $percentage > $best_percentage)
              {
                  $best_idx = $idx;
                  $best_percentage = $percentage;
              }
          }
          
          // Return the best match, or null if none was found.
          if ($best_idx === null)
              return null;
          return $vhosts[$best_idx];
      }
docs: Find the best matching VirtualHost. In case the URL used does not
  match any defined VirtualHost, this function will find the VirtualHost
  that matches the URL as close as possible, in an attempt to guess at
  which information the visitor is interested.
  
  @param URL $url The URL that was requested
  @param array $sites The configured sites
  @return VirtualHost The best matching VirtualHost in text similarity.