Note that there are some explanatory texts on larger screens.

plurals
  1. POSimplify a piece of php code, too many repetitions
    text
    copied!<p>Cant really figure out how to simplify this. What i want is to get contents of these few pages and display them on my site. Is the right way just to repeat the file_get_contents for every page i want or can i do this in another way (maybe all the web sites in one file_get_contents function ?). Also, the rest of the code just repeats its self. I this the whole code just slows down my web site a lot. If somebody is willing to lend a hand :) in this, thank you so much. </p> <pre><code>&lt;?php //Obtain Quote Info - This collects the Microsoft Stock Info $quote10 = file_get_contents('http://finance.google.com/finance/info?client=ig&amp;q=NYSE:GLD'); $quote9 = file_get_contents('http://finance.google.com/finance/info?client=ig&amp;q=INDEXEURO:PX1'); $quote8 = file_get_contents('http://finance.google.com/finance/info?client=ig&amp;q=INDEXDJSTOXX:SX5E'); $quote7 = file_get_contents('http://finance.google.com/finance/info?client=ig&amp;q=INDEXFTSE:UKX'); $quote4 = file_get_contents('http://finance.google.com/finance/info?client=ig&amp;q=INDEXNIKKEI:NI225'); $quote5 = file_get_contents('http://finance.google.com/finance/info?client=ig&amp;q=SHA:000001'); $quote4 = file_get_contents('http://finance.google.com/finance/info?client=ig&amp;q=INDEXDJX:.DJI'); $quote3 = file_get_contents('http://finance.google.com/finance/info?client=ig&amp;q=INDEXNASDAQ:.IXIC'); $quote2 = file_get_contents('http://finance.google.com/finance/info?client=ig&amp;q=INDEXSP:.INX'); $quote = file_get_contents('http://finance.google.com/finance/info?client=ig&amp;q=NASDAQ:GOOG'); $quote1 = file_get_contents('http://finance.google.com/finance/info?client=ig&amp;q=NASDAQ:YHOO'); //Remove CR's from ouput - make it one line $json = str_replace("\n", "", $quote); $json1 = str_replace("\n", "", $quote1); $json2 = str_replace("\n", "", $quote2); $json3 = str_replace("\n", "", $quote3); $json4 = str_replace("\n", "", $quote4); $json5 = str_replace("\n", "", $quote5); $json6 = str_replace("\n", "", $quote6); $json7 = str_replace("\n", "", $quote7); $json8 = str_replace("\n", "", $quote8); $json9 = str_replace("\n", "", $quote9); $json10 = str_replace("\n", "", $quote10); //echo $json['45']; //Remove //, [ and ] to build qualified string $data = substr($json, 4, strlen($json) -5); $data1 = substr($json1, 4, strlen($json1) -5); $data2 = substr($json2, 4, strlen($json2) -5); $data3 = substr($json3, 4, strlen($json3) -5); $data4 = substr($json4, 4, strlen($json4) -5); $data5 = substr($json5, 4, strlen($json5) -5); $data6 = substr($json6, 4, strlen($json6) -5); $data7 = substr($json7, 4, strlen($json7) -5); $data8 = substr($json8, 4, strlen($json8) -5); $data9 = substr($json9, 4, strlen($json9) -5); $data10 = substr($json10, 4, strlen($json10) -5); //decode JSON data $json_output = json_decode($data, true); $json_output1 = json_decode($data1, true); $json_output2 = json_decode($data2, true); $json_output3 = json_decode($data3, true); $json_output4 = json_decode($data4, true); $json_output5 = json_decode($data5, true); $json_output6 = json_decode($data6, true); $json_output7 = json_decode($data7, true); $json_output8 = json_decode($data8, true); $json_output9 = json_decode($data9, true); $json_output10 = json_decode($data10, true); //Un-remark print_r to see all array keys //print_r($json_output); //Output Stock price array key. echo "\n".$json_output['t'],"\n".$json_output['l'],"\n".$json_output['cp'];echo "&lt;br /&gt;"; echo "\n".$json_output1['t'],"\n".$json_output1['l'],"\n".$json_output1['cp'];echo "&lt;br /&gt;"; echo "\n".$json_output2['t'],"\n".$json_output2['l'],"\n".$json_output2['cp'];echo "&lt;br /&gt;"; echo "\n".$json_output3['t'],"\n".$json_output3['l'],"\n".$json_output3['cp'];echo "&lt;br /&gt;"; echo "\n".$json_output4['t'],"\n".$json_output4['l'],"\n".$json_output4['cp'];echo "&lt;br /&gt;"; echo "\n".$json_output5['t'],"\n".$json_output5['l'],"\n".$json_output5['cp'];echo "&lt;br /&gt;"; echo "\n".$json_output6['t'],"\n".$json_output6['l'],"\n".$json_output6['cp'];echo "&lt;br /&gt;"; echo "\n".$json_output7['t'],"\n".$json_output7['l'],"\n".$json_output7['cp'];echo "&lt;br /&gt;"; echo "\n".$json_output8['t'],"\n".$json_output8['l'],"\n".$json_output8['cp'];echo "&lt;br /&gt;"; echo "\n".$json_output9['t'],"\n".$json_output9['l'],"\n".$json_output9['cp'];echo "&lt;br /&gt;"; echo "\n".$json_output10['t'],"\n".$json_output10['l'],"\n".$json_output10['cp']; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload