2015-01-16 113 views
0

我想从XML这里得到的字符串:PHP的preg_match不匹配

https://gdata.youtube.com/feeds/api/videos/MZoO8QVMxkk?v=2 

我想视频标题为变量$vtitle和视频说明,作为$vdescription

<?php 
    $vinfo = @file_get_contents("https://gdata.youtube.com/feeds/api/videos/MZoO8QVMxkk?v=2"); 
    $vtitle = preg_match("/<media:title type='plain'>(.*?)<\/media:title>/", $vinfo, $match); 
    $vtitle = $match[1]; 
    $vdescription = preg_match("/<media:description type='plain'>(.*?)‬‬<\/media:description>/", $vinfo, $match); 
    $vdescription = $match[1]; 
?> 
<h1><?php echo $vtitle; ?></h1> 
<p><?php echo $vdescription; ?></p> 

输出为$ vtitle :

新复仇者预告片到达 - 漫威的复仇者联盟:奥创年龄 预告片2

为什么输出$ vdescription为空或不匹配? 请帮助这个代码有什么问题? 感谢

伊娃

+1

http://php.net/manual/en/class.domdocument.php – Bobot

回答

2

您需要使用s标志或更换.*?[\s\S]*?

s标志启用DOTALL模式。

这将是更可取的使用this这是由@ Bob0t建议作为他们更防故障,并且是专门用于处理XML,并HTML

+0

dotall因为在starttag和endtag之间有一个或多个换行符而没有dotall PCRE与linebreaks不匹配。 (点) – VolkerK

+0

谢谢你..它完美的作品:) –

+0

@EvaSilviana很高兴已经帮助:) –

0

试试这个;

$vdescription = preg_match("/<media:description type='plain'>(.*?)‬‬<\/media:description>/si", $vinfo, $match);

0

在您的正则表达式中的点字符不匹配新建成行,导致preg_match报告没有匹配。

You need to add the "s" flag to your pattern

$vdescription = preg_match("/<media:description type='plain'>(.*?)‬‬<\/media:description>/s", $vinfo, $match); 

只是重申说别人,你会使用类似的东西DOMDocument比你用正则表达式来处理XML有一个更容易的时间。