if else在变量链接输入上

时间:2014-06-19 05:18:46

标签: php variables if-statement

我有一种从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

如果用户不小心粘贴了整个网址而不是11个字符代码,那么我是否有办法使用PHP来获取代码或该网址末尾的内容(“监视”后的11个字符?v ='?

这是我提取数据的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手册也会给我们带来麻烦。你的所有答案都很棒,而且我测试的答案都有效。非常感谢你:))

5 个答案:

答案 0 :(得分:2)

试试这个,

$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;

答案 1 :(得分:1)

试试如下..

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

用输入替换$ url变量。

答案 2 :(得分:1)

$m = array();

if (preg_match ('#^(https?://www.youtube.com/watch\\?v=)?(.+)$#', $url, $m)) {
  $code = $m[2];
} else {
  /* No match */
}

代码使用Regular Expression将用户输入(主题)与模式匹配。该模式包含在您选择的一对分隔符(#)中。其余模式的工作方式如下:

  • ^匹配字符串的开头。
  • (...)创建一个子模式。
  • ?匹配前一个字符或子模式的0或1。
  • https?匹配“http”或“https”。
  • \?匹配“?”。
  • (.+)匹配1个或多个任意字符。 .匹配任何字符(换行符除外)。 +匹配前一个字符或子模式中的一个或多个。
  • $匹配字符串的结尾。

换句话说,可选择匹配httphttps基本网址,然后是视频代码。

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

答案 3 :(得分:1)

除非您想regex使用 $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]); ,否则这是一个简单的选项。

使用函数Nisse Engström's Answer,您可以执行以下操作:

$video_id

现在_gXp4hdd2pk 会返回:

$url

来自上述代码中提供的parse_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:

{{1}}

答案 4 :(得分:1)

我指示我的用户复制并粘贴整个YouTube网址 然后,我这样做:

$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