Display The Current US population using PHP

This example shows how to display the current US population figures, its an example of reading the content of another website. To learn from this the best method is to look at the source code of the url listed below and see exactly what we are searching for and how this relates to the PHP example below and how we obtain the number for our own needs.

Note CURL is better for this, we will be coming to that later 🙂

<?php
//this is the current page but this can change
$url = “http://www.census.gov/population/www/popclockus.html“;
//open the url
$fp = fopen($url, “r”);
$search = fread($fp, 32000);
fclose($fp);
//search the html of the page for the start and end tags
$search = ereg(“<div id=\”usnumber\”>(.*)</div></p>”, $search, $content);
echo “<h2>US Population</h2>”;
echo “<br>$content[1]<br>”;
?>

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*