重用 PHP 函数从 XML 文档访问数组项标签
我创建了一个网站,其中包含使用 XML 来驱动其一些内容,例如,如下所示的当前汇率。
该网站比较了三种汇率,我目前为每个城市使用单独的函数来执行相同的目标,例如返回当前汇率。
这些函数之间的唯一区别是项目标记在我从中请求数据的 XML 文档中的位置。
$exchange['rate'] = $xml->channel->item[15]->description;
请注意上面数组中的 item[15],它提供了对欧元货币的访问。在下文中,美国货币为 item[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;
}
?>
Feed URL 在另一个名为 cityConfig.php 的配置脚本中设置
<?php
// City 1 //
$city1 = 'Paris';
$exchangeRate1 = '1 Euro';
$exchangeRate1RssUrl = 'http://themoneyconverter.com/rss-feed/EUR/rss.xml';
?>
提前致谢
I have created a website that contains that uses XML to drive some of its contents, for example, the current exchange rates as shown below.
The website compares three exchange rates and I currently use a separate functions for each city that perform identical goals e.g. returning the current exchange rates.
The only difference between these functions are the positions of the item tag from the XML document that I request the data from.
$exchange['rate'] = $xml->channel->item[15]->description;
Notice item[15] in the above array which provides access to the Euro currency. In the following, the USA currency is item[56] as below.
$exchange['rate'] = $xml->channel->item[56]->description;
Any ideas if it is possible to combine the three functions into a single one to increase cohesion?
The function I use for accessing the Euro currency is:
<?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;
}
?>
The feed URL's are set in another configuration script called cityConfig.php
<?php
// City 1 //
$city1 = 'Paris';
$exchangeRate1 = '1 Euro';
$exchangeRate1RssUrl = 'http://themoneyconverter.com/rss-feed/EUR/rss.xml';
?>
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试一下这个大小 - 它使用三个字母的货币代码,并使用 XPath 来查找它们,完全消除了那些讨厌的索引。您需要提供的只是源货币的三字母代码和目的地的三字母代码,或者如果您想一次获得多个目的地,则提供一组目的地。
已修改,因此
$dest
现在是可选的。如果仅提供$source
,则返回所有汇率的数组。用法示例:
Try this on for size - it uses the three letter currency codes, and uses XPath to find them, doing away with those nasty indexes altogether. All you need to supply is the three letter code of the source currency, and the three letter code of the destination, or an array of destinations if you want to get more than one at once.
MODIFIED so that
$dest
is now optional. If only$source
is supplied, return an array of all exchange rates.Example usage:
您可以将速率函数更改为这样:
然后您可以这样调用它:
How about you change the rate function to this:
Then you can call it like this instead:
因为我还不能发表评论...helk 的答案是完美的 - 你可以更进一步,定义一些类常量或定义()常量,这样你就不会在代码中处理难以记住的数字(记住,您可能不是唯一看过此代码的人 - 您知道 15 是什么,但 Jr. Dev Newguy 不知道):
Since I can't comment yet...helk's answer is perfect - you could take it one step further and define some class constants or define()'d constants so you aren't dealing with hard to remember numbers in your code (remember, you may not be the only person who ever looks at this code - you know what 15 is, but Jr. Dev Newguy does not):