northern-lights / hostsfile-parser
Hosts file parser
dev-master / 1.0.x-dev
2025-03-29 21:17 UTC
Requires
- php: >=8.2
Requires (Dev)
- php-parallel-lint/php-console-highlighter: ^1.0
- php-parallel-lint/php-parallel-lint: ^1.4
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^3.12
- symfony/var-dumper: ^7.2
- vimeo/psalm: ^6.9
This package is auto-updated.
Last update: 2025-04-29 21:26:06 UTC
README
Lightweight and simple library for parsing hosts file. All operating systems that use this format are supported.
Install
Via Composer
$ composer require northern-lights/hostsfile-parser
It really is that easy!
Usage
<?php declare(strict_types=1); namespace NorthernLights\HostsFileParser\Example; use NorthernLights\HostsFileParser\Host; use NorthernLights\HostsFileParser\HostsFile; use NorthernLights\HostsFileParser\Parser; require __DIR__ . '/vendor/autoload.php'; $parser = new Parser(new HostsFile('/etc/hosts')); /** @var Host $host */ foreach ($parser->parse() as $host) { $domains = $host->getDomains(); echo 'Host: ' . $host->getIp() . PHP_EOL; echo sprintf('Domains: %d -> { %s }', count($domains), implode(', ', $domains)) . PHP_EOL; echo 'Line: ' . $host->getLine() . PHP_EOL; echo sprintf('Raw entry: [ %s ]', $host) . PHP_EOL; echo '--------------------------------------' . PHP_EOL; }
Please note that NorthernLights\HostsFileParser\Parser::parse() is a generator.
To parse all at once and return array, you can use Parser::parseAll() although discouraged as it could cause OOM in certain cases.
PSR-2 Standard
Library strives to comply with PSR-2 coding standards, therefore we included following commands:
$ composer check-style $ composer fix-style
Note: Second command will actually modify files
PSR-4 Standard
Library complies with PSR-4 autoloading standard
Testing
$ composer php-lint
$ composer test
License
The MIT License (MIT). Please see License File for more information.