使用preg_match捕获标签

时间:2013-02-17 01:11:35

标签: php regex preg-match matching

我使用此功能阅读并显示我想从RSS vimeo获取的信息:

$url=array('http://vimeo.com/channels/hdnature/videos/rss'); 



foreach($url as $value){
$homepage = file_get_contents($value);
$movies = new SimpleXMLElement($homepage);
      foreach($movies->channel->item as $opt){


    $title= $opt->title;
    $tittle=mysql_real_escape_string($title);

    $link=$opt->link;
    $links=mysql_real_escape_string($link); 


    $des=$opt->description;
    $dess=mysql_real_escape_string($des);
    //Use that namespace
     $namespaces = $opt->getNameSpaces(true);
    //Now we don't have the URL hard-coded
     $dc = $opt->children($namespaces['dc']); 
    //echo $dc->publisher;
    //echo $dc->creator;
   //echo $link;


    $imgpattern = '/href="(.*?)"/i';
    preg_match($imgpattern, $des, $matches);
    $imageurl['image'] = $matches[1];
    echo $imageurl['image'];

    $tag = '/">(.*?)<\/a>/';
    preg_match($tag, $des, $matches);
    $tagurl['name'] = $matches[1];
    echo $tagurl['name'];

我在<description></description>字段中有这个标记列表:

<p><strong>Tags:</strong> 
<a href="http://vimeo.com/tag:art">Art</a>, 
<a href="http://vimeo.com/tag:film">Film</a>,
<a href="http://vimeo.com/tag:movie">Movie</a>,
<a href="http://vimeo.com/tag:cinema">Cinema</a>,
<a href="http://vimeo.com/tag:creation">Creation</a>,
<a href="http://vimeo.com/tag:concept">Concept</a>,
<a href="http://vimeo.com/tag:experimental">Experimental</a>,
<a href="http://vimeo.com/tag:sound">Sound</a>,
<a href="http://vimeo.com/tag:music">Music</a>,
<a href="http://vimeo.com/tag:motion">Motion</a>,
<a href="http://vimeo.com/tag:copyleft">Copyleft</a>
and 
<a href="http://vimeo.com/tag:videoart">Videoart</a>
</p>

我搜索只在我的数据库中保存艺术,电影,电影等字样

我需要用括号保存一个列表,我尝试使用preg_match。

我有这个功能:

$tag = '/</strong>(.*?)</a></p>/';
preg_match($tag, $des, $matches);
$taglist['name'] = $matches[1];
echo $taglist['name'];

但我只收到一个错误而不是我希望的结果。

1 个答案:

答案 0 :(得分:0)

试试这个

$des = '<p><strong>Tags:</strong> 
<a href="http://vimeo.com/tag:art">Art</a>, 
<a href="http://vimeo.com/tag:film">Film</a>,
<a href="http://vimeo.com/tag:movie">Movie</a>,
<a href="http://vimeo.com/tag:cinema">Cinema</a>,
<a href="http://vimeo.com/tag:creation">Creation</a>,
<a href="http://vimeo.com/tag:concept">Concept</a>,
<a href="http://vimeo.com/tag:experimental">Experimental</a>,
<a href="http://vimeo.com/tag:sound">Sound</a>,
<a href="http://vimeo.com/tag:music">Music</a>,
<a href="http://vimeo.com/tag:motion">Motion</a>,
<a href="http://vimeo.com/tag:copyleft">Copyleft</a>
and 
<a href="http://vimeo.com/tag:videoart">Videoart</a>
</p>';
$tag = '/">(.*?)<\/a>/';
preg_match_all($tag, $des, $matches);
print_r($matches[1]);