3v4l.org

run code in 300+ PHP versions simultaneously
<?php $hostname = "lekkere.koekjes.men"; $certificates = json_decode('[{"commonName":"*.men"},{"commonName":"*.hans.nl"},{"commonName":"*.koekjes.men"}]'); $hostnameParts = explode(".", $hostname); foreach ($certificates as $certificate) { // commonName = *.lekkere.koekjes.men // hostname = www.lekkere.koekjes.men $commonName = $certificate->commonName; $commonNameParts = explode(".", $commonName); for ($i = 0; $i < max(count($commonNameParts), count($hostnameParts)); $i++) { $commonNamePart = count($commonNameParts) > ($i + 1) ? $commonNameParts[count($commonNameParts) - $i - 1] : null; $hostnamePart = count($hostnameParts) > ($i + 1) ? $hostnameParts[count($hostnameParts) - $i - 1] : null; if (! ( $commonNamePart === '*' || ($commonNamePart === null && $hostnamePart === 'www' && !in_array("*", $commonNameParts)) || ($commonNamePart === $hostnamePart) ) ) { continue 2; } } var_dump($certificate); }

preferences:
40.39 ms | 402 KiB | 5 Q