2012-02-02 23 views
0

我創建了一個網站,其中包含使用XML來驅動其內容的一些內容,例如,當前匯率如下所示。重複使用PHP函數訪問XML文檔中的數組項標記

enter image description here 該網站比較三種匯率,我目前對每個城市執行相同目標的單獨功能,例如,返回目前的匯率。

這些函數之間的唯一區別是我請求數據的XML文檔中的項目標記的位置。

$exchange['rate'] = $xml->channel->item[15]->description; 

通知項[15]上述陣列可用於訪問歐元貨幣在。在下面,美國貨幣是項目[56]如下。

$exchange['rate'] = $xml->channel->item[56]->description; 

任何想法,如果有可能將三個功能組合成一個單一的增加凝聚力?

我用來訪問歐元貨幣的功能是:

<?php 

function get_rate1(SimpleXMLElement $xml) { 

    // Match numeric values before and after decimal place 
    $exchange['rate'] = $xml->channel->item[15]->description; 
    preg_match('/([0-9]+\.[0-9]+)/', $exchange['rate'], $matches); 
    $rate = $matches[0]; 

    // Get currency type from title 
    $title['rate'] = $xml->channel->item[15]->title; 
    $title = explode('/', $title['rate']); 
    $title = $title[0]; 

    echo $rate . ' ' . $title . '<br />'; 

    return $rate; 
} 

?> 

供稿網址在叫cityConfig.php另一個配置腳本設置

<?php 

// City 1 // 
$city1 = 'Paris'; 
$exchangeRate1 = '1 Euro'; 
$exchangeRate1RssUrl = 'http://themoneyconverter.com/rss-feed/EUR/rss.xml'; 

?> 

在此先感謝

+0

你的意思是 「減少耦合」,而不是 「提高凝聚力」? – 2012-02-02 18:05:51

回答

1

如何關於您將費率函數更改爲:

<?php 

function get_rate(SimpleXMLElement $xml, $id) { 

    // Match numeric values before and after decimal place 
    $exchange['rate'] = $xml->channel->item[$id]->description; 
    preg_match('/([0-9]+\.[0-9]+)/', $exchange['rate'], $matches); 
    $rate = $matches[0]; 

    // Get currency type from title 
    $title['rate'] = $xml->channel->item[$id]->title; 
    $title = explode('/', $title['rate']); 
    $title = $title[0]; 

    echo $rate . ' ' . $title . '<br />'; 

    return $rate; 
} 

?> 

然後,你可以這樣調用它來代替:

$rate1 = get_rate($xml,15); 
$rate2 = get_rate($xml,56); 
+0

我喜歡這種方法,但是可以在我的cityConfig.php中定義$ id,就像下面的<<?php // City 1 // $ city1 ='Paris'; $ exchangeRate1 ='1歐元'; $ exchangeRate1RssUrl ='http://themoneyconverter.com/rss-feed/EUR/rss.xml'; $ id = 16; ?>' – newToJava 2012-02-02 19:20:43

1

因爲我不能評論尚未... helk的答案是完美的 - 你可以把它一步,定義一些類常量或定義() 'd常量,所以你沒有在代碼中處理難記的數字(記住,你可能不是唯一一個看過這段代碼的人 - 你知道15是什麼,但是Jr. Dev Newguy沒有):

<?php 

define('CURRENCY_USA', 56); 
define('CURRENCY_EURO', 15); 

// OR something like: 
class CurrencyHelper 
{ 
    const CURRENCY_USA = 56; 
    const CURRENCY_EURO = 15; 

    // of course, you could also define get_rate here too... 
    public function __construct(SimpleXmlElement $xml) 
    { 
     $this->xml = $xml; 
    } 

    public function get_rate($id) 
    { 
     // ... your code here 
    } 
} 

// Or, define your get_rate as originally intended: 

function get_rate(SimpleXMLElement $xml, $id) { 

    // Match numeric values before and after decimal place 
    $exchange['rate'] = $xml->channel->item[$id]->description; 
    preg_match('/([0-9]+\.[0-9]+)/', $exchange['rate'], $matches); 
    $rate = $matches[0]; 

    // Get currency type from title 
    $title['rate'] = $xml->channel->item[$id]->title; 
    $title = explode('/', $title['rate']); 
    $title = $title[0]; 

    echo $rate . ' ' . $title . '<br />'; 

    return $rate; 
} 

// Finally: 
echo get_rate($xml, CURRENCY_EURO); // define 
echo get_rate($xml, CurrencyHelper::CURRENCY_USA; // class constant 
$myCurr = new CurrencyHelper($xml); 
$myCurr->get_rate(CurrencyHelper::CURRENCY_USA); // Over-oop'd solution : P 

?> 
2

試試這個大小 - 它使用三個字母的貨幣代碼,並使用XPath找到它們,取消那些討厭的indexe完全。您需要提供的是源貨幣的三字母代碼,以及目的地的三字母代碼,或者如果您想一次獲得多個目的地的目的地數組。

改進使$dest現在是可選的。如果只提供$source,則返回所有匯率的數組。

function get_rate ($source, $dest = NULL) { 

    // Make sure source currency is upper case 
    $source = strtoupper($source); 

    // Construct the source URL 
    $url = "http://themoneyconverter.com/rss-feed/$source/rss.xml"; 

    // This will hold the results 
    $result = array(); 

    // Fetch and parse the data 
    if (!$xml = simplexml_load_file($url)) { 
    return FALSE; 
    } 

    if ($dest === NULL) { 

    // Get all <item> nodes and loop them 
    foreach ($xml->xpath("//item") as $item) { 

     // Find the code of this currency 
     $currencyParts = explode('/', $item->title); 

     // Get the value of this currency 
     $result[$currencyParts[0]] = (preg_match('/([0-9]+\.[0-9]+)/', $item->description, $matches)) ? (float) $matches[0] : FALSE; 

    } 

    } else { 

    // Loop the destination currencies 
    foreach ((array) $dest as $currency) { 

     // Make sure destination currencies are upper case 
     $currency = strtoupper($currency); 

     // Perform an XPath search for this currency 
     $nodes = $xml->xpath("//item[title='$currency/$source']/description"); 

     // If we found the currency and could extract the value, add it to the 
     // result array as a float 
     $result[$currency] = (count($nodes) === 1 && preg_match('/([0-9]+\.[0-9]+)/', $nodes[0], $matches)) ? (float) $matches[0] : FALSE; 

    } 

    } 

    // If only a single value was requested return it, otherwise return the array 
    return (count($result) === 1) ? array_shift($result) : $result; 

} 

用法示例:

$result = get_rate('GBP', 'USD'); 
var_dump($result); 
/* 
    float(1.58014) 
*/ 

$result = get_rate('GBP', array('USD', 'EUR')); 
var_dump($result); 
/* 
    array(2) { 
    ["USD"]=> 
    float(1.58014) 
    ["EUR"]=> 
    float(1.20236) 
    } 
*/ 

$result = get_rate('GBP'); 
var_dump($result); 
/* 
    array(64) { 
    ["AED"]=> 
    float(5.80445) 
    ["ARS"]=> 
    float(6.85316) 
    ["AUD"]=> 
    float(1.47589) 
    ["BBD"]=> 
    float(3.16103) 
    ["BHD"]=> 
    float(0.59427) 
    ["BOB"]=> 
    float(10.92135) 
    ["BRL"]=> 
    float(2.72171) 
    ["CAD"]=> 
    float(1.57968) 
    ["CHF"]=> 
    float(1.44883) 
    ["CLP"]=> 
    float(759.35947) 
    ["CNY"]=> 
    float(9.96753) 
    ["COP"]=> 
    float(2840.97943) 
    ["CZK"]=> 
    float(30.15863) 
    ["DKK"]=> 
    float(8.97219) 
    ["EGP"]=> 
    float(9.53446) 
    ["EUR"]=> 
    float(1.20265) 
    ["HKD"]=> 
    float(12.24901) 
    ["HUF"]=> 
    float(350.91425) 
    ["IDR"]=> 
    float(14121.92063) 
    ["ILS"]=> 
    float(5.87877) 
    ["INR"]=> 
    float(77.48491) 
    ["ISK"]=> 
    float(194.46687) 
    ["JMD"]=> 
    float(136.31954) 
    ["JOD"]=> 
    float(1.12059) 
    ["JPY"]=> 
    float(120.36272) 
    ["KES"]=> 
    float(132.28924) 
    ["KRW"]=> 
    float(1763.3828) 
    ["KWD"]=> 
    float(0.43897) 
    ["LBP"]=> 
    float(2382.62959) 
    ["LKR"]=> 
    float(180.02093) 
    ["LTL"]=> 
    float(4.1525) 
    ["LVL"]=> 
    float(0.84522) 
    ["MAD"]=> 
    float(13.39206) 
    ["MXN"]=> 
    float(20.24582) 
    ["MYR"]=> 
    float(4.77078) 
    ["NAD"]=> 
    float(12.10631) 
    ["NGN"]=> 
    float(253.27781) 
    ["NOK"]=> 
    float(9.21948) 
    ["NPR"]=> 
    float(123.97585) 
    ["NZD"]=> 
    float(1.89597) 
    ["OMR"]=> 
    float(0.6077) 
    ["PAB"]=> 
    float(1.58052) 
    ["PEN"]=> 
    float(4.25316) 
    ["PHP"]=> 
    float(67.48803) 
    ["PKR"]=> 
    float(142.95779) 
    ["PLN"]=> 
    float(5.03909) 
    ["QAR"]=> 
    float(5.75308) 
    ["RON"]=> 
    float(5.23271) 
    ["RUB"]=> 
    float(47.73085) 
    ["SAR"]=> 
    float(5.92694) 
    ["SEK"]=> 
    float(10.66422) 
    ["SGD"]=> 
    float(1.96993) 
    ["THB"]=> 
    float(48.79218) 
    ["TRY"]=> 
    float(2.77931) 
    ["TWD"]=> 
    float(46.6742) 
    ["UAH"]=> 
    float(12.71293) 
    ["USD"]=> 
    float(1.58052) 
    ["UYU"]=> 
    float(30.74107) 
    ["VEF"]=> 
    float(6.79622) 
    ["VND"]=> 
    float(33119.73602) 
    ["XAF"]=> 
    float(788.88394) 
    ["XCD"]=> 
    float(4.2674) 
    ["XOF"]=> 
    float(788.88394) 
    ["ZAR"]=> 
    float(12.10631) 
    } 
*/ 
+0

+1對於真棒解決方案=) – 2012-02-02 19:54:21