2011-10-27 18 views
-4

我目前擁有這段代碼。如何在PHP數組中進行替換?

<?php 
$username = 'EMAIL ADDRESS'; 
$password = 'EMAIL ADDRESS'; 
$opts = array(
    'http' => array(
     'method' => 'GET', 
     'header' => sprintf("Authorization: Basic %s\r\nUser-Agent: Foobar!", base64_encode($username.':'.$password)) 
) 
); 

$context = stream_context_create($opts); 
libxml_set_streams_context($context); 
$xml = new SimpleXMLElement(
    "https://mail.google.com/mail/feed/atom", 
    null, 
    true); 
print_r($xml); 
?> 

我得到下面的結果,當我運行PHP代碼

SimpleXMLElement對象([@屬性] =>數組([版] => 0.3)[標題] =>的Gmail - EMAIL地址的收件箱[標語] => Gmail收件箱中的新郵件[fullcount] => 1 [link] => SimpleXMLElement Object([@attributes] => Array([rel] => alternate [href] =>http://mail.google.com/mail [類型] => text/html))[modified] => 2011-10-27T20:12:38Z [entry] => SimpleXMLElement Object([title] => SUBJECT [link] => SimpleXMLElement Object([@attributes] = > Array([rel] => alternate [href] => LINK [type] => text/html))[modified] => 2011-10-26T1 5:45:11Z [issued] => 2011-10-26T15:45:11Z [id] => tag:gmail.google.com,2004:1383746934228011210 [author] => SimpleXMLElement Object([name] => Webs [電子郵件] => EMAIL)))

我希望它這樣的結果不顯示 「SimpleXMLElement對象」 等,並只顯示實際有用的信息(主題,郵件內容,日期等)

我試過使用str_replace,但它不起作用。

+3

-1這個問題。這個問題沒有顯示任何研究工作。 @ 01jayss - print_r的行爲*與文檔說明應該*完全相同。如果它沒有做你想做的,也許你需要一個不同的功能。 – Farray

+2

抱怨說,你的香草冰淇淋(print_r輸出)嚐起來就像牛排(你的XML對象),讓你從宇宙中的每個餐廳,包括Milliways都笑出來。 –

回答

1

試着這麼做:

echo "Title: ".$xml->title.", tagline: ".$xml->tagline; 

您可能需要根據XML的格式一些額外的路徑添加到上面。

您也可以嘗試

print_r((array)$xml); 

爲:

<?php 
$xml = "<x><title>Blah</title><fred>bob</fred></x>"; 
$s = simplexml_load_string($xml); 
print_r((array)$s); 

,輸出:

Array 
(
    [title] => Blah 
    [fred] => bob 
) 
0

使用的print_r()將只給你一個人可讀的輸出。您可以將其轉換爲XML或JSON以更符合您的需求。

嘗試轉換爲json。

$json = json_encode($xml); 
echo $json;