0

这是我的代码。

$xml = simplexml_load_file("http://www.metacafe.com/api/item/8373557/", NULL, LIBXML_NOCDATA);
$thumbnail = $xml->xpath( "/rss/channel/item/media:thumbnail/@url" );
$duration = $xml->xpath( "/rss/channel/item/media:content/@duration" );
$title = $xml->xpath ( "/rss/channel/item/title");
$desc = $xml->xpath ( "/rss/channel/item/description/");

这是 XML 文件内容:

<?xml version="1.0" encoding="utf-8"?>
        <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" source="Metacafe">
            <title>Metacafe</title>
            <channel>
                    <title></title>
                    <link>http://www.metacafe.com/watch/8373557/</link>
                    <image>
                        <url>http://s.mcstatic.com/Images/MCLogo4RSS.jpg</url>
                        <link>http://www.metacafe.com</link>
                        <title>Metacafe</title>
                        <height>65</height>
                        <width>229</width>
                    </image>
                    <description></description>
                    <item>
            <id>8373557</id>
            <author>Revision3</author>
            <title>Make Your Photos Look Pro with Pix: Pixel Mixer for IOS and Android - Snapp</title>
            <link>http://www.metacafe.com/watch/8373557/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp/</link>
                    <rank>3.70</rank>
            <category>Science &amp; Tech</category>
            <description>
            <![CDATA[
                    <a href="http://www.metacafe.com/watch/8373557/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp/"><img src="http://s6.mcstatic.com/thumb/8373557/22302729/4/directors_cut/0/1/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp.jpg?v=3" align="right" border="0" alt="Make Your Photos Look Pro with Pix: Pixel Mixer for IOS and Android - Snapp" vspace="4" hspace="4" width="134" height="78" /></a>
                    <p>
                    Create your own professional quality images with this free app from developer colonO.  Pix: Pixel Mixer is simple, beautiful, free and available on both iOS and Android.                   <br>Ranked <strong>3.70</strong> / 5 | 4971 views | <a href="http://www.metacafe.com/watch/8373557/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp/">0 comments</a><br/>
                    </p>
                    <p>
                        <a href="http://www.metacafe.com/watch/8373557/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp/"><strong>Click here to watch the video</strong></a> (01:02)<br/>
                        Submitted By:                       <a href="http://www.metacafe.com/channels/Revision3/">Revision3</a><br/>
                        Tags:
                        <a href="http://www.metacafe.com/topics/pictures/">Pictures</a>&nbsp;<a href="http://www.metacafe.com/topics/pixel_mixer/">Pixel Mixer</a>&nbsp;<a href="http://www.metacafe.com/topics/photo_filters/">Photo Filters</a>&nbsp;<a href="http://www.metacafe.com/topics/filters_for/">Filters For</a>&nbsp;<a href="http://www.metacafe.com/topics/ios_and/">Ios And</a>&nbsp;<a href="http://www.metacafe.com/topics/android/">Android</a>&nbsp;<a href="http://www.metacafe.com/topics/ios_review/">Ios Review</a>&nbsp;<a href="http://www.metacafe.com/topics/instagram_filters/">Instagram Filters</a>&nbsp;                        <br/>
                        Categories: <a href='http://www.metacafe.com/videos/science_and_tech/'>Science &amp; Tech</a>                   </p>
                ]]>
            </description>
                        <guid isPermaLink="true">http://www.metacafe.com/watch/8373557/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp/</guid>
            <pubDate>16-Apr-12 +0000</pubDate>
                        <media:player url="http://www.metacafe.com/watch/8373557/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp/" />
                        <media:content url="http://www.metacafe.com/fplayer/8373557/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp.swf" type="application/x-shockwave-flash" medium="video" height="360" width="640" duration="62" />             <media:thumbnail url="http://s6.mcstatic.com/thumb/8373557/22302729/4/catalog_item5/0/1/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp.jpg?v=3" />
            <media:title>Make Your Photos Look Pro with Pix: Pixel Mixer for IOS and Android - Snapp</media:title>
            <media:keywords>Pictures,Pixel Mixer,Photo Filters,Filters For,Ios And,Android,Ios Review,Instagram Filters</media:keywords>
                        <media:description>Create your own professional quality images with this free app from developer colonO.  Pix: Pixel Mixer is simple, beautiful, free and available on both iOS and Android.</media:description>
                        <media:credit>Revision3</media:credit>
                            <media:rating scheme="urn:simple">nonadult</media:rating>
                </item>
            </channel></rss>

我想提取标签内容,不知道应该用哪个表达式来打印?我成功获取缩略图、持续时间、标题,但未能在 CDATA / 内部描述中获取标签。

有什么帮助吗?

4

1 回答 1

2

在第一步中,您需要使用或使用您所拥有的 xpath 表达式来获取<description>标记的内容。$xml->rss->channel->item->description然后,您在此内容上使用正则表达式来获取带有标签名称的链接。正则表达式应该是这样的:

~<a href="http://www.metacafe.com/topics/(?P<foobar>[^"]+)/">(?P<tag>[^<]+)</a>~

另一种方法是请求一个 API,您可以使用该 API 从条目中获取标签。

于 2012-06-08T19:40:35.420 回答