0

XML を使用してそのコンテンツの一部を駆動する Web サイトを作成しました。たとえば、以下に示す現在の為替レートです。

ここに画像の説明を入力 この Web サイトでは 3 つの為替レートを比較しています。現在、現在の為替レートを返すなど、同じ目的を実行する各都市に対して個別の関数を使用しています。

これらの関数の唯一の違いは、データを要求する XML ドキュメントの item タグの位置です。

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

ユーロ通貨へのアクセスを提供する上記の配列のitem[15]に注目 してください。以下では、米国の通貨はitem[56]です。

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

結束力を高めるために、3 つの機能を 1 つの機能に組み合わせることが可能であれば、何かアイデアはありますか?

ユーロ通貨にアクセスするために使用する関数は次のとおりです。

<?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;
}

?>

フィード URL は、cityConfig.php という別の構成スクリプトで設定されます。

<?php

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

?>

前もって感謝します

4

3 に答える 3

2

サイズを試してみてください。3 文字の通貨コードを使用し、XPath を使用してそれらを見つけ、厄介なインデックスを完全に排除します。入力する必要があるのは、ソース通貨の 3 文字のコードと目的地の 3 文字のコード、または一度に複数を取得する場合は目的地の配列だけです。

変更されたため、$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)
  }
*/
于 2012-02-02T18:38:00.487 に答える
1

私はまだコメントできないので...ヘルクの答えは完璧です-さらに一歩進んで、いくつかのクラス定数またはdefine()された定数を定義して、コード内の覚えにくい数字を扱わないようにすることができます(覚えておいてください) 、このコードを見たことがあるのはあなただけではないかもしれません - あなたは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

?>
于 2012-02-02T17:55:18.437 に答える
1

rate 関数を次のように変更します。

<?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);
于 2012-02-02T17:37:01.780 に答える