2015-01-15 62 views
1

在此先感謝您閱讀此。添加一些文本到PHP代碼 - 爲傻瓜

我對PHP或編碼一無所知,我一直在試圖遵循一些互聯網指南,爲一段代碼添加一些文字。該代碼用於執行一些自動貨幣轉換,並將其嵌入到在彈出窗口中打開的文章中。

我拼命地試圖在每個單獨的轉換前添加幾個字。我已經添加了下面的代碼 - 輸出是最後三行,並且我想在每行前添加一些文本(每行不同的文本),但是我一直遇到麻煩,我可以' t看起來沒錯。

我會非常感激任何想法,我甚至不確定這是可能的。

我想在最後三行的前面添加一些內容。

這可能嗎?我會感謝任何和所有的想法,在此感謝您的時間。

最良好的祝願, 本

<?php 
// ######################################################################### 
// September 09, 2012 
// Real time PHP currency converter function with quotes from Yahoo Finance 
// Written by Leonard Whistler 
// [email protected] 
// ######################################################################### 

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); 

?> 

回答

0

試試這個:

<?php 
// ######################################################################### 
// September 09, 2012 
// Real time PHP currency converter function with quotes from Yahoo Finance 
// Written by Leonard Whistler 
// [email protected] 
// ######################################################################### 

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); 

?> 
+0

似乎有點多餘的傳遞給函數,當你可以只是做一個「回聲」無論「; currencyExchange( 「25」,$ USD,$ KRW);'。可能更有意義的是返回結果,而不是在函數中回顯它:'回顯「任何內容」。 currencyExchange(「25」,$ usd,$ krw);' – Tom 2015-01-15 16:26:13

+0

這需要更多的更改... currencyExchange迴應答案,因此在這種情況下,您的建議會在代碼後面打印「任何」,而不是在前面。 .. – patrick 2015-01-15 16:27:43

+0

我假設'$ timestamp'是他想要添加到輸出中的。 – Tom 2015-01-15 16:28:43

0

雖然最後三行調用貨幣轉換,輸出實際上是所有在這一行做了:

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

您可以添加y文本,您想要該行的開始,並且它將反映在每次轉換的輸出中。例如:

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

發生了什麼是最後三行中的每一行運行函數currencyExchance()。該功能使用echo命令打印輸出。 echo命令只是輸出文本(你把它放在引號中)或php變量。

希望有所幫助。

0

如果你想改變輸出到包括在前面一些文本功能currencyExchange的輸出是在底部,這樣的:

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

這樣你就可以添加這些類似的話,每次你調用currencyExchange它會添加這些話

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

或修改方法類似@patrick建議之前,我可以打後到接受可變3個字。 :)

0

您的函數立即回聲的結果,這是恕我直言,不理想。所以,而不是引入第四個參數,我會做以下。

<?php 
// ######################################################################### 
// September 09, 2012 
// Real time PHP currency converter function with quotes from Yahoo Finance 
// Written by Leonard Whistler 
// [email protected] 
// ######################################################################### 

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>"; 

我認爲應該將函數的目的從你對結果做的事情中分離出來。