添加一些文本到PHP代码 - 为傻瓜

时间:2015-01-15 16:20:04

标签: php

提前感谢您阅读本文。

我对php或编码一无所知,我一直试图按照一些互联网指南将一些文字添加到一段代码中。该代码用于执行一些自动货币转换,并且这些转换嵌入在弹出窗口中打开的文章中。

我拼命想在每次单独转换前添加几个单词。我已添加下面的代码 - 输出是最后三行,我想在每行前面添加一些文本(每行不同的文字),但我一直遇到麻烦,我似乎无法做对。

我非常感谢任何想法,我甚至不确定它是否可能。

我想在最后三行的前面添加一些东西。

这可能吗?我会感激任何和所有的想法,提前感谢您的时间。

祝福, 本

 <?php
// #########################################################################
// September 09, 2012
// Real time PHP currency converter function with quotes from Yahoo Finance
// Written by Leonard Whistler
// lwhistler@gmail.com
// #########################################################################

function currencyExchange($amount,$baseCurrency,$quoteCurrency) {
$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$baseCurrency[0]$quoteCurrency[0]=X&f=sl1d1t1c1ohgv&e=.csv", "r");
$exchangeRate = fread($open, 2000);
fclose($open);
$exchangeRate = str_replace("\"", "", $exchangeRate);
$exchangeRate = explode(",", $exchangeRate);
$results = ($exchangeRate[1]*$amount);
$results = number_format ($results, 0);
$amount = number_format ($amount);
$timeStamp = date('F d, Y');
$timeStamp = "$timeStamp $exchangeRate[3]";

echo "$amount $baseCurrency[1] = $results $quoteCurrency[1]<br>";
$timestamp;
}

// for additional currency ticker symbols visit: http://finance.yahoo.com/currency-converter
$usd = array('USD','USD');
$eur = array('EUR','Euro');
$jpy = array('JPY','Japanese Yen');
$gbp = array('GBP','British Pounds');
$aud = array('AUD','Australian Dollars');
$chf = array('CHF','Swiss Francs');
$cad = array('CAD','Canadian Dollars');
$krw = array('KRW','KRW');

// amount, base currency, quote currency.
currencyExchange("25",$usd,$krw);
currencyExchange("20",$usd,$krw);
currencyExchange("39",$usd,$krw);

?> 

4 个答案:

答案 0 :(得分:0)

试试这个:

<?php
// #########################################################################
// September 09, 2012
// Real time PHP currency converter function with quotes from Yahoo Finance
// Written by Leonard Whistler
// lwhistler@gmail.com
// #########################################################################

function currencyExchange($pre,$amount,$baseCurrency,$quoteCurrency) {
$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$baseCurrency[0]$quoteCurrency[0]=X&f=sl1d1t1c1ohgv&e=.csv", "r");
$exchangeRate = fread($open, 2000);
fclose($open);
$exchangeRate = str_replace("\"", "", $exchangeRate);
$exchangeRate = explode(",", $exchangeRate);
$results = ($exchangeRate[1]*$amount);
$results = number_format ($results, 0);
$amount = number_format ($amount);
$timeStamp = date('F d, Y');
$timeStamp = "$timeStamp $exchangeRate[3]";

echo "$pre $amount $baseCurrency[1] = $results $quoteCurrency[1]<br>";
$timestamp;
}

// for additional currency ticker symbols visit: http://finance.yahoo.com/currency-converter
$usd = array('USD','USD');
$eur = array('EUR','Euro');
$jpy = array('JPY','Japanese Yen');
$gbp = array('GBP','British Pounds');
$aud = array('AUD','Australian Dollars');
$chf = array('CHF','Swiss Francs');
$cad = array('CAD','Canadian Dollars');
$krw = array('KRW','KRW');

// amount, base currency, quote currency.
currencyExchange("whatever","25",$usd,$krw);
currencyExchange("somethingelse","20",$usd,$krw);
currencyExchange("blabla","39",$usd,$krw);

?> 

答案 1 :(得分:0)

虽然最后三行调用了货币转换,但输出实际上都是在这一行上完成的:

echo "$amount $baseCurrency[1] = $results $quoteCurrency[1]<br>";

您可以将所需的任何文本添加到该行的开头,它将反映在每次转换的输出中。例如:

echo "Look some extra text $amount $baseCurrency[1] = $results $quoteCurrency[1]<br>";

最后三行中的每一行都运行了函数currencyExchance()。该函数使用echo命令打印输出。 echo命令只输出文本(你把它放在引号中)或php变量。

希望有所帮助。

答案 2 :(得分:0)

如果您尝试更改输出以包含前面的一些文本,则函数currencyExchange的输出位于底部:

echo "$amount $baseCurrency[1] = $results $quoteCurrency[1]<br>";

所以你可以添加这些单词,每次调用currencyExchange时它都会添加这些单词

echo "Some Extra Words $amount $baseCurrency[1] = $results $quoteCurrency[1]<br>";

或者修改方法以接受变量3个单词,如@patrick建议,然后才能发帖。 :)

答案 3 :(得分:0)

你的功能立即回应结果,这是恕我直言并不理想。因此,我将执行以下操作,而不是引入第四个参数。

<?php
// #########################################################################
// September 09, 2012
// Real time PHP currency converter function with quotes from Yahoo Finance
// Written by Leonard Whistler
// lwhistler@gmail.com
// #########################################################################

function currencyExchange($amount,$baseCurrency,$quoteCurrency) {
$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$baseCurrency[0]$quoteCurrency[0]=X&f=sl1d1t1c1ohgv&e=.csv", "r");
$exchangeRate = fread($open, 2000);
fclose($open);
$exchangeRate = str_replace("\"", "", $exchangeRate);
$exchangeRate = explode(",", $exchangeRate);
$results = ($exchangeRate[1]*$amount);
$results = number_format ($results, 0);
$amount = number_format ($amount);
$timeStamp = date('F d, Y');
$timeStamp = "$timeStamp $exchangeRate[3]";

return "$amount $baseCurrency[1] = $results $quoteCurrency[1]";
$timestamp;
}

// for additional currency ticker symbols visit: http://finance.yahoo.com/currency-converter
$usd = array('USD','USD');
$eur = array('EUR','Euro');
$jpy = array('JPY','Japanese Yen');
$gbp = array('GBP','British Pounds');
$aud = array('AUD','Australian Dollars');
$chf = array('CHF','Swiss Francs');
$cad = array('CAD','Canadian Dollars');
$krw = array('KRW','KRW');

// amount, base currency, quote currency.
echo "the result is ".currencyExchange("25",$usd,$krw);
echo "Blabla ".currencyExchange("20",$usd,$krw);
echo "TEST: ".currencyExchange("39",$usd,$krw);

?> 

这使您可以更灵活地输出输出,例如将结果包装在表中。

echo "<table>";
echo "<tr><td>first request </td><td>".currencyExchange("25",$usd,$krw)."</td></tr>";
echo "<tr><td>next</td><td>".currencyExchange("20",$usd,$krw)."</td></tr>";
echo "<tr><td>and so</td><td>".currencyExchange("39",$usd,$krw)."</td></tr>";
echo "</table>";

我认为应该将功能目的与您对结果的目的分开。