problem with php and regex
i saw some things look a like but not the same, i hope you can assist:
i am trying to execute the first result from youtube search results by
parse youtube search result page and find this: (belong to the first video)
<img alt="Thumbnail" src="http://i4.ytimg.com/vi/kUJLn7645Zs/default.jpg">
then i want to get the video code which is this: (kUJLn7645Zs)
this is my code and something doesn't work, please assist
$res = file_get_contents("http://www.youtube.com/results?search_query=take+me");
preg_match_all('/<img\s[a-zA-Z0-9=\'\":\/.()_@&\s]*>开发者_如何学运维/',$res,$matches);
$str = $matches[0][0];
$tempArray = array();
$tempArray = explode('/',$str);
echo $tempArray[count($tempArray) - 2];
Why not :
if(preg_match_all('/\<img [^\>]* src\="(https?:)?\/\/i[0-9]\.ytimg\.com\/vi\/([_a-zA-Z0-9-]+)\/default\.jpg" ?[^\>]*\>/',$res,$matches))
{
$firstVid= $matches[2][0];
}
else
{
$firstVid= "";
}
print_r($matches);
?
it's hard to use regex to parse html.
Try using a pre-made framework like SimpleHtmlDom
I would rather us
explode("/", "myvar");
And just take the part that interests me
this is the complete answer.
thank you all and a big thank you to Enki :)
$res = file_get_contents("http://www.youtube.com/results?search_query=take+me");
if(preg_match_all('/\<img [^\>]* src\="(https?:)?\/\/i[0-9]\.ytimg\.com\/vi\/([_a-zA-Z0-9-]+)\/default\.jpg" ?[^\>]*\>/',$res,$matches))
{
$firstVid= $matches[0][0];
}
else
{
$firstVid= "";
}
$tempArray = array();
$tempArray = explode('/',$firstVid);
echo $tempArray[count($tempArray) - 2];
精彩评论