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
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments