Search:

Categories



Line Counter example

This is a simple line counter example which will count the lines in a text file

<?php
function LineCounter($file)
 {
    /* open the file  */
    $fh = fopen( $file, “r” );

    $count = 0;

    while(fgets($fh))
    {
        $count++;
    }
    fclose($fh);
    return $count;
 }
 
 /*change to a file on your system*/
echo LineCounter(“phonebook.php”);

?>

Share

User chooses stock quotes

This example lets the user choose what stock quotes they would like to see

2 parts , first part is our form , I called it yahoocsv1.php but ideally you would have all of this on the one page and the page would update with the necessary information

<form action=”yahoocsv2.php” method =”post” target=”_blank”>
Input your ticker symbol below<br>
<input type = “text” name =”request”><br>
<input type = “submit” value = “display quotes”>
</form>

second part is yahoo2csv.php

<?php

//stock quote script
//this is the url for Microsoft’s stock quote , we are opening it for reading
$fp = fopen (“http://finance.yahoo.com/d/quotes.csv?s=$request&f=sl1d1t1c1ohgv&e=.csv”,”r”);
//this uses the fgetcsv function to store the quote info in the array $data
$data = fgetcsv ($fp, 1000, “,”)
?>
<!– this is our table which displays the stock info –>
<!– we access the individual items by using $data[0]–>
<html>
<head>
</head>
<body>
<table>
<tr><td>description</td><td>latest figure</td><tr>
<tr><td>symbol</td><td><?php echo $data[0] ?></td></tr>
<tr><td>last price</td><td><?php echo $data[1] ?></td></tr>
<tr><td>date</td><td><?php echo $data[2] ?></td></tr>
<tr><td>time</td><td><?php echo $data[3] ?></td></tr>
<tr><td>change</td><td><?php echo $data[4] ?></td></tr>
<tr><td>open</td><td><?php echo $data[5] ?></td></tr>
<tr><td>high</td><td><?php echo $data[6] ?></td></tr>
<tr><td>low</td><td><?php echo $data[7] ?></td></tr>
<tr><td>volume</td><td><?php echo $data[8] ?></td></tr>
</table>
<?php
//close the filehandle $fp
fclose ($fp);
?>

Share

World population

<?php
$url = “http://www.census.gov/ipc/www/popclockworld.html“;
$fp = fopen($url, “r”);
$search = fread($fp, 32000);
fclose($fp);
$search = ereg(“<div id=\”worldnumber\”>(.*)</div><p></p>”, $search, $content);
echo “<h2>World Population</h2>”;
echo “<div align=\”center\”><br>$content[1]<br></div>”;
?>

Share

US population example

 

<?php
//the address with the figures, this may change in the future
$url = “http://www.census.gov/population/www/popclockus.html“;
$fp = fopen($url, “r”);
$search = fread($fp, 32000);
fclose($fp);
//the html that surrounds the figures we want, this may change
$search = ereg(“<div id=\”usnumber\”>(.*)</div></p>”, $search, $content);
echo “<h2>US Population</h2>”;
echo “<br>$content[1]<br>”;
?>

Share

Latest stock market information

This example is getting information for the Dow Jones stock market

<?php
//this is the url of our csv file
$url = “http://finance.yahoo.com/d/quotes.csv?s=^DJI&f=sl1d1t1c1ohgv&e=.csv”;
//open it for reading
$fp = fopen($url , “r”);
//if no connection exists display error message
if (!fp)
{
echo “could not connect to the site”;
}
else
{
//store the csv file info in the array $data
$data = fgetcsv($fp,1000,”,”);
//close the file
fclose($fp);
?>
<table>
<tr><td>description</td><td>latest figure</td><tr>
<tr><td>Market</td><td><?php echo $data[0] ?></td></tr>
<tr><td>last price</td><td><?php echo $data[1] ?></td></tr>
<tr><td>date</td><td><?php echo $data[2] ?></td></tr>
<tr><td>time</td><td><?php echo $data[3] ?></td></tr>
<tr><td>change</td><td><?php echo $data[4] ?></td></tr>
<tr><td>open</td><td><?php echo $data[5] ?></td></tr>
<tr><td>high</td><td><?php echo $data[6] ?></td></tr>
<tr><td>low</td><td><?php echo $data[7] ?></td></tr>
</table>
<?php
}
?>

Share

Display all links on a web page

Display all links on a web page

<?php

$page = 0;
$URL = “http://www.getphp.net/”;
$page = @fopen($URL, “r”);
print(“Links at $URL<BR>\n”);
print(“<UL>\n”);
while(!feof($page)) {
$line = fgets($page, 255);
while(eregi(“HREF=\”[^\”]*\””, $line, $match)) {
print(“<LI>”);
print($match[0]);
print(“<BR>\n”);
$replace = ereg_replace(“\?”, “\?”, $match[0]);
$line = ereg_replace($replace, “”, $line);
}
}
print(“</UL>\n”);
fclose($page);

?>

Share