Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Config/HostsFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static function loadFromPathBlocking($path = null)
public function __construct($contents)
{
// remove all comments from the contents
$contents = preg_replace('/ *#.*/', '', strtolower($contents));
$contents = preg_replace('/[ \t]*#.*/', '', strtolower($contents));

$this->contents = $contents;
}
Expand Down Expand Up @@ -131,7 +131,7 @@ public function getHostsForIp($ip)

$names = array();
foreach (preg_split('/\r?\n/', $this->contents) as $line) {
$parts = preg_split('/\s+/', $line);
$parts = preg_split('/\s+/', $line, null, PREG_SPLIT_NO_EMPTY);
$addr = array_shift($parts);

// remove IPv6 zone ID (`fe80::1%lo0` => `fe80:1`)
Expand Down
9 changes: 9 additions & 0 deletions tests/Config/HostsFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ public function testReverseLookup()
$this->assertEquals(array(), $hosts->getHostsForIp('192.168.1.1'));
}

public function testReverseSkipsComments()
{
$hosts = new HostsFile("# start\n#127.0.0.1 localhosted\n127.0.0.2\tlocalhost\t# example.com\n\t127.0.0.3\t\texample.org\t\t");

$this->assertEquals(array(), $hosts->getHostsForIp('127.0.0.1'));
$this->assertEquals(array('localhost'), $hosts->getHostsForIp('127.0.0.2'));
$this->assertEquals(array('example.org'), $hosts->getHostsForIp('127.0.0.3'));
}

public function testReverseNonIpReturnsNothing()
{
$hosts = new HostsFile('127.0.0.1 localhost');
Expand Down