使用 PHP 从 XML 提要中隔离单个项目

发布于 2024-08-23 17:48:24 字数 238 浏览 13 评论 0原文

您好,感谢您的浏览。

我试图从以下链接获取 1 GBP = USD 之后括号中的值,如何使用 SimpleXML 执行此操作?

http://www.sloomedia.com/currency/feeds/GBP.xml

谢谢, 本.

Hi and thanks for looking.

I'm trying to get just the value in brackets after 1 GBP = USD from the following link, how do I do this with SimpleXML?

http://www.sloomedia.com/currency/feeds/GBP.xml

Thanks,
Ben.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤城病女 2024-08-30 17:48:24

您可以尝试使用 xpath :

$xml = new SimpleXMLElement($string);

/* On cherche <item><title> */
$result = $xml->xpath('/item/title');

while(list( , $node) = each($result)) {
    $matches = array();
    preg_match('/\((.*)\)/', $node, $matches);


    //do wathever you need with result
    //echo $matches[1];
}

我还没有测试快速编写的正则表达式,您可以找到更好的东西或使用 substr 因为“1GBP = XXX”的长度在您的文件中是恒定的

You can try with xpath :

$xml = new SimpleXMLElement($string);

/* On cherche <item><title> */
$result = $xml->xpath('/item/title');

while(list( , $node) = each($result)) {
    $matches = array();
    preg_match('/\((.*)\)/', $node, $matches);


    //do wathever you need with result
    //echo $matches[1];
}

I haven't tested the regexp quickly written, you can find something better or use substr as the length of '1GBP = XXX' is constant in your file

空宴 2024-08-30 17:48:24

为了“隔离”(或者更确切地说,选择)正确的节点,您可以使用 XPath 函数 starts-with()

选择节点后,您仍然需要解析其内容。为此,您可以在 PHP 中使用任何类型的字符串操作函数。

$rss = simplexml_load_file('http://www.sloomedia.com/currency/feeds/GBP.xml');

$nodes = $rss->xpath('//item[starts-with(title, "1 GBP = USD")]');

if (empty($nodes))
{
    die('No GBP = USD nodes');
}

// Now you got the right node in $nodes[0], you just have to extract the value in parentheses
// The simple way -- 13 is the number of characters in "1 GBP = USD ("
$usd = substr($nodes[0]->title, 13, -1);

// The flexible way
if (!preg_match('#\\(([0-9]+\\.[0-9]+)\\)#', $nodes[0]->title, $m))
{
    die('Could not parse the value');
}
$usd = $m[1];

// If you want to parse every item
foreach ($rss->channel->item as $item)
{
    if (!preg_match('#1 ([A-Z]+) = ([A-Z]+) \\(([0-9]+\\.[0-9]+)\\)#', $item->title, $m))
    {
        echo 'Could not parse ', $item->title, "\n";
        continue;
    }

    echo '1 ', $m[1], ' = ', $m[3], ' ', $m[2], "\n";
}

In order to "isolate" (or rather, select) the right node, you can use the XPath function starts-with().

After selecting the node, you still have to parse its contents. For that, you can use any kind of string-manipulation function in PHP.

$rss = simplexml_load_file('http://www.sloomedia.com/currency/feeds/GBP.xml');

$nodes = $rss->xpath('//item[starts-with(title, "1 GBP = USD")]');

if (empty($nodes))
{
    die('No GBP = USD nodes');
}

// Now you got the right node in $nodes[0], you just have to extract the value in parentheses
// The simple way -- 13 is the number of characters in "1 GBP = USD ("
$usd = substr($nodes[0]->title, 13, -1);

// The flexible way
if (!preg_match('#\\(([0-9]+\\.[0-9]+)\\)#', $nodes[0]->title, $m))
{
    die('Could not parse the value');
}
$usd = $m[1];

// If you want to parse every item
foreach ($rss->channel->item as $item)
{
    if (!preg_match('#1 ([A-Z]+) = ([A-Z]+) \\(([0-9]+\\.[0-9]+)\\)#', $item->title, $m))
    {
        echo 'Could not parse ', $item->title, "\n";
        continue;
    }

    echo '1 ', $m[1], ' = ', $m[3], ' ', $m[2], "\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文