Reputation: 69
I want to get string from xml here:
https://gdata.youtube.com/feeds/api/videos/MZoO8QVMxkk?v=2
I want to get video title as variable $vtitle
and video description as $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>
Output for $vtitle:
New Avengers Trailer Arrives - Marvel's Avengers: Age of Ultron Trailer 2
Why output for $vdescription is empty or doesn't match? Please help what's wrong with this code? Thanks
Eva
Upvotes: 1
Views: 1055
Reputation: 1425
The dot character in your regular expression is not matching new lines, which is causing preg_match to report no matches.
You need to add the "s" flag to your pattern.
$vdescription = preg_match("/<media:description type='plain'>(.*?)<\/media:description>/s", $vinfo, $match);
Just to reiterate what others have said, you will have a much easier time using something like DOMDocument to process XML than you will with regular expressions.
Upvotes: 0
Reputation: 59232
You need to use s
flag or replace .*?
with [\s\S]*?
s
flag enables dotall mode.
It would be more advisable to use this which was suggested by @Bob0t as they're more fail-proof and are specifically built for handling XMLs and HTML
Upvotes: 2
Reputation: 391
Try this one;
$vdescription = preg_match("/<media:description type='plain'>(.*?)<\/media:description>/si", $vinfo, $match);
Upvotes: 0