2012-01-22 102 views
1

好吧,我做了一個return simplexml_load_string($data, 'SimpleXMLElement', LIBXML_COMPACT | LIBXML_NOCDATA | LIBXML_NOBLANKS | LIBXML_NOEMPTYTAG);並解析xml響應。filter xml CDATA response

問題是[說明]的內容真的搞砸了,我需要選擇我需要的數據。

[description] => 
    <a href="http://www.metacafe.com/watch/cb-YpE1z5IhjWrmCM62DSTU8jQ9X4IZryVR/the_dish_with_doc_willoughby/"><img src="http://s4.mcstatic.com/thumb/8000947/21507982/4/directors_cut/0/1/the_dish_with_doc_willoughby.jpg?v=8" align="right" border="0" alt="THE Dish with Doc Willoughby" vspace="4" hspace="4" width="134" height="78" /></a> 
    <p> 
     Doc Willoughby, guru of &quot;America's Test Kitchen,&quot; stopped by &quot;CBS The Morning: Saturday&quot; to share his ultimate dish with Rebecca Jarvis and Jeff Glor: Roast Beef Tenderloin with Dried Fruit and Nut Stuffing.     <br>Ranked <strong>4.00</strong>/5 | 2 views | <a href="http://www.metacafe.com/watch/cb-YpE1z5IhjWrmCM62DSTU8jQ9X4IZryVR/the_dish_with_doc_willoughby/">0 comments</a><br/> 
    </p> 
    <p> 
      <a href="http://www.metacafe.com/watch/cb-YpE1z5IhjWrmCM62DSTU8jQ9X4IZryVR/the_dish_with_doc_willoughby/"><strong>Click here to watch the video</strong></a> (04:58)<br/> 
     Submitted By:      <a href="http://www.metacafe.com/channels/CBS/">CBS</a><br/> 
     Tags: 
     <a href="http://www.metacafe.com/topics/cbsepisode/">Cbsepisode</a>&nbsp;<a href="http://www.metacafe.com/topics/dish/">Dish</a>&nbsp;<a href="http://www.metacafe.com/topics/doc_willoughby/">Doc Willoughby</a>&nbsp;<a href="http://www.metacafe.com/topics/america%27s_test_kitchen/">America's Test Kitchen</a>&nbsp;<a href="http://www.metacafe.com/topics/roast_beef_tenderloin/">Roast Beef Tenderloin</a>&nbsp;<a href="http://www.metacafe.com/topics/dried_fruit/">Dried Fruit</a>&nbsp;<a href="http://www.metacafe.com/topics/nut_stuffing/">Nut Stuffing</a>&nbsp;<a href="http://www.metacafe.com/topics/cbs_this_morning/">CBS This Morning</a>&nbsp;      <br/> 
    Categories: <a href='http://www.metacafe.com/videos/news_and_events/'>News &amp; Events</a>      </p> 

正如你所看到的,它真的搞砸了,我想知道我怎樣才能得到例如第一<p>的數據,直到
排名......’
和標籤也

編輯:

確定繼承人我使用的PHP代碼:

 $dom = new DOMDocument(); 
     @$dom->loadHTML($result->description); // or you can use loadXML 
     $dom->normalizeDocument(); 
     /*$dom->resolveExternals = false; 
     $dom->substituteEntities = false;*/ 
     $xml = simplexml_import_dom($dom); 
     $data['viewData']['data']['description'] = $xml; 

 $paragraph = $dom->getElementsByTagName('p'); -> this doesn't work 
     //$xml = simplexml_import_dom($dom); 
     $data['viewData']['data']['description'] = $paragraph; 

這裏是輸出:

[description] => SimpleXMLElement Object 
       (
        [body] => SimpleXMLElement Object 
         (
          [a] => SimpleXMLElement Object 
           (
            [@attributes] => Array 
             (
              [href] => http://www.metacafe.com/watch/cb-YpE1z5IhjWrmCM62DSTU8jQ9X4IZryVR/the_dish_with_doc_willoughby/ 
             ) 

            [img] => SimpleXMLElement Object 
             (
              [@attributes] => Array 
               (
                [src] => http://s4.mcstatic.com/thumb/8000947/21507982/4/directors_cut/0/1/the_dish_with_doc_willoughby.jpg?v=8 
                [align] => right 
                [border] => 0 
                [alt] => THE Dish with Doc Willoughby 
                [vspace] => 4 
                [hspace] => 4 
                [width] => 134 
                [height] => 78 
               ) 

             ) 

           ) 

          [p] => Array 
           (
            [0] => 
        Doc Willoughby, guru of "America's Test Kitchen," stopped by "CBS The Morning: Saturday" to share his ultimate dish with Rebecca Jarvis and Jeff Glor: Roast Beef Tenderloin with Dried Fruit and Nut Stuffing.     Ranked/5 | 2 views | 
            [1] => SimpleXMLElement Object 
             (
              [a] => Array 
               (
                [0] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [href] => http://www.metacafe.com/watch/cb-YpE1z5IhjWrmCM62DSTU8jQ9X4IZryVR/the_dish_with_doc_willoughby/ 
                   ) 

                  [strong] => Click here to watch the video 
                 ) 

                [1] => CBS 
                [2] => Cbsepisode 
                [3] => Dish 
                [4] => Doc Willoughby 
                [5] => America's Test Kitchen 
                [6] => Roast Beef Tenderloin 
                [7] => Dried Fruit 
                [8] => Nut Stuffing 
                [9] => CBS This Morning 
                [10] => News & Events 
               ) 

              [br] => Array 
               (
                [0] => SimpleXMLElement Object 
                 (
                 ) 

                [1] => SimpleXMLElement Object 
                 (
                 ) 

                [2] => SimpleXMLElement Object 
                 (
                 ) 

               ) 

             ) 

           ) 

是否有「製造更漂亮輸出」的方法嗎?我的意思是更好的命令...我也嘗試使用getElementsByTagName('p')但沒有成功

+0

說實話,沒什麼......我現在想使用HTML解析器一些'預浸-match' – Alex

+0

爲什麼不加載描述? –

+0

嗯,好主意Dor ...我現在就試試吧 – Alex

回答

1

看看this我以前的答案。它將字符串解析爲XML對象,以便您可以輕鬆訪問任何節點。

在你的情況下拿到第一款:

$dom = new DOMDocument(); 
$dom->loadHTML($description); // $description - your string from the feed 
if (!$dom) { 
    die('Error loading HTML string.'); 
} 

$xml = simplexml_import_dom($dom); 
$p = (string)$xml->body->p; 

echo '<pre>'; print_r($p); echo '</pre>'; 
+0

檢查編輯,並非常感謝信息 – Alex

+0

不要對同一個問題重複您的答案。 – salathe

+0

@salathe你在說什麼? – Alex