如果else在变量链接输入上


if else on variable link input

我有一种从API链接中提取Youtube视频数据的方法。我使用Wordpress时遇到了一个障碍。

为了提取缩略图、视图、上传器和视频标题,我需要用户在观看结束时输入11个字符的代码?v=_______。这是为用户提供的特定说明,但如果他们忽略它并粘贴整个url呢?

// the url 'code' the user should input.
_gXp4hdd2pk
// the wrong way, when the user pastes the whole url.
https://www.youtube.com/watch?v=_gXp4hdd2pk

如果用户不小心粘贴了整个URL,而不是11个字符的代码,那么有没有一种方法可以使用PHP获取代码或该URL末尾的内容("watch?v='?"后的11个字符

以下是我提取数据的PHP代码:

// $url is the code at the end of 'watch?v=' that the user inputs
$url = get_post_meta ($post->ID, 'youtube_url', $single = true);
// $code is a variable for placing the $url in a youtube link so I can output it to an API link
$code = 'http://www.youtube.com/watch?v=' . $url;
// $code is called at the end of this oembed code, allowing me to decode json data and pull elements from json to echo in my html
// echoed output returns json file. example: http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=_gXp4hdd2pk
$json = file_get_contents('http://www.youtube.com/oembed?url='.urlencode($code));

我在找。。。

"如果用户输入代码,请使用此代码块,否则,如果用户输入整个url,请使用不同的代码块,不然将抛出错误。">

或者。。。如果他们使用整个URL,PHP是否只能使用该URL的特定部分。。。?

编辑:谢谢你的回答!我是PHP的新手,所以感谢大家的耐心等待。图形设计师很难学习PHP,甚至阅读PHP手册也会让我们头疼。你所有的答案都很好,我测试过的都有效。非常感谢:(

试试这个,

$code = 'https://www.youtube.com/watch?v=_gXp4hdd2pk';
if (filter_var($code, FILTER_VALIDATE_URL) == TRUE) { 
   // if `$code` is valid url
   $code_arr = explode('?v=', $code);
   $query_str = explode('&', $code_arr[1]);
   $new_code =  $query_str[0];
} else {
    // if `$code` is not a valid url like '_gXp4hdd2pk' 
    $new_code = $code;
}
echo $new_code;

这里有一个简单的选项,除非您想像Nisse Engström的Answer那样使用regex

使用函数parse_url(),您可以执行以下操作:

    $url = 'https://www.youtube.com/watch?v=_gXp4hdd2pk&list=RD_gXp4hdd2pk#t=184';
    $split = parse_url('https://www.youtube.com/watch?v=_gXp4hdd2pk&list=RD_gXp4hdd2pk#t=184');
    $params = explode('&', $split['query']);
    $video_id = str_replace('v=', '', $params[0]);

现在$video_id将返回:

_gXp4hdd2pk

来自在上述代码中提供的CCD_ 4。

我建议您阅读parse_url()文档,以确保您理解并掌握所有内容:-(


更新请发表评论。

您可以使用类似的方法来确保解析的值是有效的URL:

// this will check if valid url
if (filter_var($code, FILTER_VALIDATE_URL)) {
    // its valid as it returned true
    // so run the code
    $url = 'https://www.youtube.com/watch?v=_gXp4hdd2pk&list=RD_gXp4hdd2pk#t=184';
    $split = parse_url('https://www.youtube.com/watch?v=_gXp4hdd2pk&list=RD_gXp4hdd2pk#t=184');
    $params = explode('&', $split['query']);
    $video_id = str_replace('v=', '', $params[0]);
} else {
    // they must have posted the video code as the if check returned false.
    $video_id = $url;
}

只需按如下方式尝试即可。。

  $url =" https://www.youtube.com/watch?v=_gXp4hdd2pk";
$url= explode('?v=', $url);
$endofurl = end($url);
echo $endofurl;

用输入替换$url变量。

我指示用户复制并粘贴整个youtube url
然后,我这样做:

$video_url = 'https://www.youtube.com/watch?v=_gXp4hdd2pk'; // this is from user input
$parsed_url = parse_url($video_url);
parse_str($parsed_url['query'], $query);
$vidID = isset($query['v']) ? $query['v'] : NULL;
$url = "http://gdata.youtube.com/feeds/api/videos/". $vidID; // this is used for the Api
$m = array();
if (preg_match ('#^(https?://www.youtube.com/watch''?v=)?(.+)$#', $url, $m)) {
  $code = $m[2];
} else {
  /* No match */
}

该代码使用正则表达式将用户输入(主题(与模式相匹配。该模式包含在您选择的一对分隔符(#(中。模式的其余部分是这样工作的:

  • ^匹配字符串的开头
  • (...)创建一个子模式
  • ?匹配前一个字符或子模式的0或1
  • https?匹配"http"或"https">
  • '?匹配"?">
  • CCD_ 12匹配一个或多个任意字符。.匹配任何字符(换行符除外(。+匹配前面的一个或多个字符或子模式
  • $匹配字符串的末尾

换言之,可选地匹配httphttps基本URL,后跟视频代码。

然后将匹配项写入$m$m[0]包含整个字符串,$m[1]包含第一个子模式(基本URL(,$m[2]包含第二个子模式(代码(。