Next: reject.php
Up: Appendix I: Code
Previous: portfolio.php
<?php
//various methods of displaying data for stocks and indices
/*basic information, additional information in column, additional information
for interface*/
//retrieve($symbols) obtains basic data relating to the variable
//$symbols is the symbol for an index or listing on Yahoo! Finance
function retrieve($symbols)
{
//obtain a page containing data regarding $symbols
$url = "http://finance.yahoo.com/q?s=$symbols&d=t";
if (!($fp = fopen($url, "r")))
{
echo "Unable to open page.";
exit;
}
//if opening is successful, read 100000 bytes from opened page
$contents = fread($fp, 100000);
//close page
fclose($fp);
//if the value of $symbols is less than 1000 points or dollars
//for indices and listings (companies and corporations in indices, respectively
if (preg_match("/(<b>[0-9]+\.[0-9]+<\/b>)/", $contents, $quote))
{
//print value of index or listing
echo "<TD WIDTH=64 VALIGN=RIGHT><FONT SIZE=2 FACE=arial>$quote[1]</FONT></TD>";
//print change in points or dollars
//green for positive, white for negative
if (preg_match("/(Change<br><font color=ff0020>\-[0-9]+\.[0-9]+)/",
$contents, $quote))
{
//extra specificity of string searched due to possibility of negative EPS
//and positive change, resulting in two numbers displayed as change
$quote[1] = substr($quote[1], 29);
echo "<TD WIDTH=48 VALIGN=RIGHT><FONT SIZE=2 FACE=arial COLOR=FFFFFF>$quote[1]</FONT></TD>";
}
if (preg_match("/(\+[0-9]+\.[0-9]+)/", $contents, $quote))
echo "<TD WIDTH=48 VALIGN=RIGHT><FONT SIZE=2 FACE=arial COLOR=00FF00>$quote[1]</FONT></TD>";
//print change in percent
//green for positve, white for negative
if (preg_match("/(\-[0-9]+\.[0-9]+\%)/", $contents, $quote))
echo "<TD WIDTH=64 VALIGN=RIGHT><FONT SIZE=2 FACE=arial COLOR=FFFFFF>$quote[1]</FONT></TD></TR>";
if (preg_match("/(\+[0-9]+\.[0-9]+\%)/", $contents, $quote))
echo "<TD WIDTH=64 VALIGN=RIGHT><FONT SIZE=2 FACE=arial COLOR=00FF00>$quote[1]</FONT></TD></TR>";
}
//if the value of $symbols exceeds 1000 points (indices only)
else if (preg_match("/(<b>[0-9]+\,[0-9]+\.[0-9]+<\/b>)/", $contents, $quote))
{
//print value of index
echo "<TD WIDTH=72 VALIGN=RIGHT><FONT SIZE=2 FACE=arial>$quote[1]</FONT></TD>";
//print chage in points or dollars
//green for positive, white for negative
if (preg_match("/(\-[0-9]+\.[0-9]+)/", $contents, $quote))
echo "<TD WIDTH=64 VALIGN=RIGHT><FONT SIZE=2 FACE=arial COLOR=FFFFFF>$quote[1]</FONT></TD>";
if (preg_match("/(\+[0-9]+\.[0-9]+)/", $contents, $quote))
echo "<TD WIDTH=64 VALIGN=RIGHT><FONT SIZE=2 FACE=arial COLOR=00FF00>$quote[1]</FONT></TD>";
//print change in percent
//green for positive, white for negative
if (preg_match("/(\-[0-9]+\.[0-9]+\%)/", $contents, $quote))
echo "<TD WIDTH=64 VALIGN=RIGHT><FONT SIZE=2 FACE=arial COLOR=FFFFFF>$quote[1]</FONT></TD></TR>";
if (preg_match("/(\+[0-9]+\.[0-9]+\%)/", $contents, $quote))
echo "<TD WIDTH=64 VALIGN=RIGHT><FONT SIZE=2 FACE=arial COLOR=00FF00>$quote[1]</FONT></TD></TR>";
}
//if the value of $symbols cannot be determined
else
{
echo "No quote available.<P>";
}
}
//end function retrieve($symbols)
//if $symbols is not an index
//display technical data
function techdata($symbols)
{
//obtain a page containing data regarding $symbols
$url = "http://finance.yahoo.com/q?s=$symbols&d=t";
if (!($fp = fopen($url, "r")))
{
echo "Unable to open page.";
exit;
}
//if opening is successful, read 100000 bytes from opened page
$contents = fread($fp, 100000);
//close page
fclose($fp);
if ($symbols != "^DJI" && $symbols != "^IXIC" && $symbols != "^GSPC"
&& $symbols != "^dji" && $symbols != "^ixic" && $symbols != "^gspc")
{
echo "<TABLE CELLSPACING=0 CELLPADDING=0>";
//Market
//NYSE - New York Stock Exchange
//NasdaqNM - Nasdaq National Market
//AMEX - American Stock Exchange
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>Exchange</TD>";
if (preg_match("/(NYSE:$symbols)/", $contents, $quote))
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>NYSE</TD></TR>";
if (preg_match("/(NasdaqNM:$symbols)/", $contents, $quote))
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>NASDAQ</TD></TR>";
if (preg_match("/(AMEX:$symbols)/", $contents, $quote))
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>American</TD></TR>";
//P/E Ratio - Price to Earnings
if (preg_match("/(P\/E<br>[0-9]+\.[0-9]+)/", $contents, $quote))
{
$quote[1] = substr($quote[1], 7);
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>P/E</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>$quote[1]</TD></TR>";
}
else {
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>P/E</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>N/A</TD></TR>";
}
//PEG - P/E / Growth
if (preg_match("/(PEG<br>[0-9]+\.[0-9]+)/", $contents, $quote))
{
$quote[1] = substr($quote[1], 7);
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>PEG</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>$quote[1]</TD></TR>";
}
else {
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>PEG</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>N/A</TD></TR>";
}
//EPS - Earnings Per Share
/*/
if (preg_match("/(<small>(ttm)<\/small><br>[0-9]+\.[0-9]+)/",
$contents, $quote))
{
//$quote[1] = substr($quote[1], 11);
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>Earnings</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>$quote</TR>";
}
else if (preg_match("/(<small>(ttm)<\/small><br>\-[0-9]+\.[0-9]+)/",
$contents, $quote))
{
//$quote[1] = substr($quote[1], 11);
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>Earnings</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>$quote</TR>";
}
else {
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>Earnings</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>N/A</TR>";
}
/*/
//Market Capitalization
if (preg_match("/(Mkt Cap<br>[0-9]+\.[0-9]+)/", $contents, $quote))
{
$quote[1] = substr($quote[1], 11);
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>Market Cap</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>$$quote[1] ";
if (preg_match("/(Mkt Cap<br>[0-9]+\.[0-9]+B)/", $contents, $quote))
echo "Billion</TD></TR>";
if (preg_match("/(Mkt Cap<br>[0-9]+\.[0-9]+M)/", $contents, $quote))
echo "Million</TD></TR>";
}
else {
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>Market Cap</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>N/A</TD></TR>";
}
//Volume - Shares Traded (Daily)
if (preg_match("/(Volume<br>[0-9]+\,[0-9]+\,[0-9]+)/", $contents, $quote)
|| preg_match("/(Volume<br>[0-9]+\,[0-9]+)/", $contents, $quote)
|| preg_match("/(Volume<br>[0-9]+)/", $contents, $quote))
{
$quote[1] = substr($quote[1], 10);
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>Volume</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>$quote[1]</TD></TR>";
}
else {
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>Volume</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>N/A</TD></TR>";
}
//Volume - Shares Traded (Daily)
if (preg_match("/(Avg Vol<br>[0-9]+\,[0-9]+\,[0-9]+)/", $contents, $quote)
|| preg_match("/(Avg Vol<br>[0-9]+\,[0-9]+)/", $contents, $quote))
{
$quote[1] = substr($quote[1], 11);
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>Average Volume</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>$quote[1]</TD></TR>";
}
else {
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>Average Volume</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>N/A</TD></TR>";
}
echo "</TABLE><BR>";
}
}
//if $symbols is not an index
//display technical data for final interface
function data($symbols, $c, $r)
{
//obtain a page containing data regarding $symbols
$url = "http://finance.yahoo.com/q?s=$symbols&d=t";
if (!($fp = fopen($url, "r")))
{
echo "Unable to open page.";
exit;
}
//if opening is successful, read 100000 bytes from opened page
$contents = fread($fp, 100000);
//close page
fclose($fp);
if ($symbols != "^DJI" && $symbols != "^IXIC" && $symbols != "^GSPC"
&& $symbols != "^dji" && $symbols != "^ixic" && $symbols != "^gspc")
{
echo "<TABLE CELLSPACING=0 CELLPADDING=0>";
//Market
//NYSE - New York Stock Exchange
//NasdaqNM - Nasdaq National Market
//AMEX - American Stock Exchange
echo "<TR><TD WIDTH=80><FONT FACE=arial SIZE=2>Exchange</TD>";
if (preg_match("/(NYSE:$symbols)/", $contents, $quote))
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>NYSE</TD>";
if (preg_match("/(NasdaqNM:$symbols)/", $contents, $quote))
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>NASDAQ</TD>";
if (preg_match("/(AMEX:$symbols)/", $contents, $quote))
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>American</TD>";
//Market Capitalization
if (preg_match("/(Mkt Cap<br>[0-9]+\.[0-9]+)/", $contents, $quote))
{
$quote[1] = substr($quote[1], 11);
echo "<TD WIDTH=120><FONT FACE=arial SIZE=2>Market Cap</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>$$quote[1] ";
if (preg_match("/(Mkt Cap<br>[0-9]+\.[0-9]+B)/", $contents, $quote))
echo "Billion</TD></TR>";
if (preg_match("/(Mkt Cap<br>[0-9]+\.[0-9]+M)/", $contents, $quote))
echo "Million</TD></TR>";
}
else {
echo "<TD WIDTH=120><FONT FACE=arial SIZE=2>Market Cap</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>N/A</TD></TR>";
}
//P/E Ratio - Price to Earnings
if (preg_match("/(P\/E<br>[0-9]+\.[0-9]+)/", $contents, $quote))
{
$quote[1] = substr($quote[1], 7);
echo "<TR><TD WIDTH=80><FONT FACE=arial SIZE=2>P/E</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>$quote[1]</TD>";
}
else {
echo "<TR><TD WIDTH=80><FONT FACE=arial SIZE=2>P/E</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>N/A</TD>";
}
//PEG - P/E / Growth
if (preg_match("/(PEG<br>[0-9]+\.[0-9]+)/", $contents, $quote))
{
$quote[1] = substr($quote[1], 7);
echo "<TD WIDTH=120><FONT FACE=arial SIZE=2>PEG</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>$quote[1]</TD></TR>";
}
else {
echo "<TD WIDTH=120><FONT FACE=arial SIZE=2>PEG</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>N/A</TD></TR>";
}
//EPS - Earnings Per Share
/*/
if (preg_match("/(<small>(ttm)<\/small><br>[0-9]+\.[0-9]+)/",
$contents, $quote))
{
//$quote[1] = substr($quote[1], 11);
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>Earnings</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>$quote</TR>";
}
else if (preg_match("/(<small>(ttm)<\/small><br>\-[0-9]+\.[0-9]+)/",
$contents, $quote))
{
//$quote[1] = substr($quote[1], 11);
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>Earnings</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>$quote</TR>";
}
else {
echo "<TR><TD WIDTH=120><FONT FACE=arial SIZE=2>Earnings</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>N/A</TR>";
}
/*/
//Volume - Shares Traded (Daily)
if (preg_match("/(Volume<br>[0-9]+\,[0-9]+\,[0-9]+)/", $contents, $quote)
|| preg_match("/(Volume<br>[0-9]+\,[0-9]+)/", $contents, $quote)
|| preg_match("/(Volume<br>[0-9]+)/", $contents, $quote))
{
$quote[1] = substr($quote[1], 10);
echo "<TR><TD WIDTH=80><FONT FACE=arial SIZE=2>Volume</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>$quote[1]</TD>";
}
else {
echo "<TR><TD WIDTH=80><FONT FACE=arial SIZE=2>Volume</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>N/A</TD>";
}
//Volume - Shares Traded (Daily)
if (preg_match("/(Avg Vol<br>[0-9]+\,[0-9]+\,[0-9]+)/", $contents, $quote)
|| preg_match("/(Avg Vol<br>[0-9]+\,[0-9]+)/", $contents, $quote))
{
$quote[1] = substr($quote[1], 11);
echo "<TD WIDTH=120><FONT FACE=arial SIZE=2>Average Volume</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>$quote[1]</TD></TR>";
}
else {
echo "<TD WIDTH=120><FONT FACE=arial SIZE=2>Average Volume</TD>";
echo "<TD WIDTH=100><FONT FACE=arial SIZE=2>N/A</TD></TR>";
}
if ($c < $r - 1)
echo "<TR><TD><BR></TD></TR>";
echo "</TABLE>";
}
}
?>
Joseph B. Hess
2003-06-13