diff --git a/src/Hostname.php b/src/Hostname.php index a97d787d1..31a5329e3 100644 --- a/src/Hostname.php +++ b/src/Hostname.php @@ -2053,6 +2053,7 @@ public function isValid($value) } // Match TLD against known list + $removedTld = false; if ($this->getTldCheck()) { if (! in_array(strtolower($this->tld), $this->validTlds) && ! in_array($this->tld, $this->validTlds)) { @@ -2063,6 +2064,7 @@ public function isValid($value) // We have already validated that the TLD is fine. We don't want it to go through the below // checks as new UTF-8 TLDs will incorrectly fail if there is no IDN regex for it. array_pop($domainParts); + $removedTld = true; } /** @@ -2083,6 +2085,9 @@ public function isValid($value) // Check each hostname part $check = 0; $lastDomainPart = end($domainParts); + if (! $removedTld) { + $lastDomainPart = prev($domainParts); + } foreach ($domainParts as $domainPart) { // Decode Punycode domain names to IDN if (strpos($domainPart, 'xn--') === 0) { diff --git a/test/HostnameTest.php b/test/HostnameTest.php index 562a3a5c2..fa71e1ab2 100644 --- a/test/HostnameTest.php +++ b/test/HostnameTest.php @@ -173,6 +173,22 @@ public function testValidatorHandlesUnderscoresInDomainsCorrectly($input, $asser $this->$assertion($validator->isValid($input), implode("\n", $validator->getMessages())); } + /** + * Ensure the underscore character tests work as expected when not using tld check + * + * @dataProvider domainsWithUnderscores + * @param string $input + * @param string $assertion + */ + public function testValidatorHandlesUnderscoresInDomainsWithoutTldCheckCorrectly($input, $assertion) + { + $validator = new Hostname([ + 'useTldCheck' => false, + 'allow' => Hostname::ALLOW_DNS, + ]); + $this->$assertion($validator->isValid($input), implode("\n", $validator->getMessages())); + } + /** * Ensures that getMessages() returns expected default value *