Fishtrap

php and other stuff I know

Turn your hosts file into a home page

| 0 comments

I’ve been working on a project that has many different URLs. As the project is not yet live each URL is mapped in my hosts file. To save remembering all urls and give me a quick link I used a php script to create a page that listed every word as a link on the page. I now have this as my home page giving me quick links to all the urls I’ve got mapped.

<?php
$filename = '/etc/hosts';
$contents = file($filename);

echo '<pre>';

foreach ($contents as $line) {
if (lineIsComment($line)) {
echo $line;
} else {
$words = preg_split ('/\s/', $line);
    foreach ($words as $word) {
    echo wordToLink($word);
    }
}

}

echo '</pre>';

function wordToLink($word) {
    return sprintf('<a href="http://%s">%s</a> '.PHP_EOL, $word, $word);
}

function lineIsComment($line) {
    return 0 === strpos($line, '#');
}

Leave a Reply

Required fields are marked *.

*