PHP: check $msg for containing yt link
In the messages users write with eachother, I wish to turn Youtube links into the youtube thumbnail of it+title.
So how can I check if $msg contains a youtube video link, and if it does, it should take the video id (?v=) of it, and run this:
$.g开发者_运维问答etScript( 'http://gdata.youtube.com/feeds/api/videos/$videoid?v=2&alt=json-in-script&callback=youtubeFetchDataCallback' );
How can this be done?
Already resolved partially here: parse youtube video id using preg_match
EDIT alternatively you could use parse_url() in PHP check the host is youtube and if it is read the query string and split into key/value pairs and read the "v" value
EDIT 2
<?php
$url = "http://www.youtube.com/watch?v=QDe6MZQjpho";
$url = parse_url($url);
if($url['host'] == "www.youtube.com") {
parse_str($url['query'], $output);
$videoID = $output['v'];
} else {
echo "not youtube.com";
}
?>
EDIT 3 Another way
<?php
$url = "http://www.youtube.com/watch?v=QDe6MZQjpho";
if(preg_match("#http://(.*)\.youtube\.com/watch\?v=(.*)(&(.*))?#", $url, $matches)){
$videoID = $matches[2];
} else {
echo "not youtube.com";
}
?>
精彩评论