BrowscapPHP\Parser\Helper\GetPatternInterface::getPatterns PHP Method

getPatterns() public method

Gets some possible patterns that have to be matched against the user agent. With the given user agent string, we can optimize the search for potential patterns: - We check the first characters of the user agent (or better: a hash, generated from it) - We compare the length of the pattern with the length of the user agent (the pattern cannot be longer than the user agent!)
public getPatterns ( string $userAgent ) : Iterator
$userAgent string
return Iterator
    public function getPatterns($userAgent);

Usage Example

Beispiel #1
0
 /**
  * Gets the browser data formatr for the given user agent
  * (or null if no data avaailble, no even the default browser)
  *
  * @param  string                  $userAgent
  * @return FormatterInterface|null
  */
 public function getBrowser($userAgent)
 {
     $userAgent = strtolower($userAgent);
     $formatter = null;
     foreach ($this->patternHelper->getPatterns($userAgent) as $patterns) {
         $patternToMatch = '/^(?:' . str_replace("\t", ')|(?:', $patterns) . ')$/i';
         if (!preg_match($patternToMatch, $userAgent)) {
             continue;
         }
         // strtok() requires less memory than explode()
         $pattern = strtok($patterns, "\t");
         while ($pattern !== false) {
             $pattern = str_replace('[\\d]', '(\\d)', $pattern);
             $quotedPattern = '/^' . $pattern . '$/i';
             $matches = [];
             if (preg_match($quotedPattern, $userAgent, $matches)) {
                 // Insert the digits back into the pattern, so that we can search the settings for it
                 if (count($matches) > 1) {
                     array_shift($matches);
                     foreach ($matches as $oneMatch) {
                         $numPos = strpos($pattern, '(\\d)');
                         $pattern = substr_replace($pattern, $oneMatch, $numPos, 4);
                     }
                 }
                 // Try to get settings - as digits have been replaced to speed up the pattern search (up to 90 faster),
                 // we won't always find the data in the first step - so check if settings have been found and if not,
                 // search for the next pattern.
                 $settings = $this->dataHelper->getSettings($pattern);
                 if (count($settings) > 0) {
                     $formatter = $this->formatter;
                     $formatter->setData($settings);
                     break 2;
                 }
             }
             $pattern = strtok("\t");
         }
     }
     return $formatter;
 }
GetPatternInterface