简体   繁体   English

将PHP变量设置为XML值?

[英]Set PHP variable to XML value?

I want to use XML in PHP. 我想在PHP中使用XML。 Till now I use this PHP (for a little 'rss reader'): 到现在为止,我使用以下PHP(针对一些“ rss读者”):

<?php

$items = 25;
$rss     = simplexml_load_file('rss.xml');

echo "<h1>{$rss->channel->title}</h1>\n";

for($i=0;$i<$items;$i+=1)
   {  
   echo "<div class=\"channel\"><a href=\"{$rss->channel->item[$i]->link}\" target=\"_blank\">{$rss->channel->item[$i]->title}</a></div>\n";
   echo "<div class=\"description\">{$rss->channel->item[$i]->description}</div><br>\n";
   }

?>

I'm using this to get a value from the XML: {$rss->channel->item[$i]->link} . 我正在使用它从XML中获取值: {$rss->channel->item[$i]->link} But I want to set a PHP value called $link to this value. 但是我想将一个名为$link的PHP值设置为此值。

I tried something like $link = $rss->channel->item[$i]->link , but that doesn't work. 我尝试了类似$link = $rss->channel->item[$i]->link ,但这不起作用。 So how can I set the PHP variable $link to the value in the xml file? 那么如何将PHP变量$link设置为xml文件中的值?

You didn't specify what "does not work" means exactly. 您没有指定“不起作用”的确切含义。 So I'll make a guess here: 所以我在这里猜一个:

$link = (string)$rss->channel->item[$i]->link;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM