Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 忽略视频URL中的换行符_Php_Regex_Url_Youtube - Fatal编程技术网

Php 忽略视频URL中的换行符

Php 忽略视频URL中的换行符,php,regex,url,youtube,Php,Regex,Url,Youtube,我想用iframe替换YouTube URL。因此,我使用从不同网站复制的正则表达式,从YouTube URL获取视频ID 它不能完美地工作。如果有换行符,它不会获取整个视频ID。也不会从URL获取&feature=share 例如: 输入: http://www.youtube.com/attribution_link?a=_NDwn20sMog&u=/watch?v=zl-GC1 6vBm4&feature=share 正则表达式: /(?:https?:\/\/)?(?:

我想用iframe替换YouTube URL。因此,我使用从不同网站复制的正则表达式,从YouTube URL获取视频ID

它不能完美地工作。如果有换行符,它不会获取整个视频ID。也不会从URL获取&feature=share

例如:

输入:

http://www.youtube.com/attribution_link?a=_NDwn20sMog&u=/watch?v=zl-GC1
6vBm4&feature=share
正则表达式:

/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/\S*(?:(?:\/e(?:mbed))?\/|attribution_link\?a=.+?watch.+?v(?:%|=)|watch\?(?:\S*?&?v\=))|youtu\.be\/)([a-zA-Z0-9_-]{6,11})/i
输出:

Array
(
    [0] => zl-GC1
)
Array
(
    [0] => http://www.youtube.com/attribution_link?a=_NDwn20sMog&u=/watch?v=zl-GC1
)

通过清除潜在的空白然后进行匹配来简化

这是一个我的意思的例子

<?php 

$test = <<<TEST
http://www.youtube.com/attribution_link?a=_NDwn20sMog&u=/watch?v=zl-GC1
6vBm4&feature=share
TEST;

$test = preg_replace('/\s/', '', $test); // NOTE: Scrub potential whitespace.
// NOTE: (&.*)* added to the end of the original pattern to match the
// rest of the query string if any.
preg_match('/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/\S*(?:(?:\/e(?:mbed))?\/|attribution_link\?a=.+?watch.+?v(?:%|=)|watch\?(?:\S*?&?v\=))|youtu\.be\/)([a-zA-Z0-9_-]{6,11})(&.*)*/', $test, $matches);

var_dump($matches);

?>

您可以在。

中测试此方法。为了帮助解决此问题,您需要准确地提供您需要从注册表中获得的信息。这可能很接近:谢谢您的回复。对不起,我的问题不清楚。J0e3gan的回答成功了。谢谢J0e3gan!清除空白并将&.*添加到模式的末尾就达到了目的。
array(3) {
  [0]=>
  string(90) "http://www.youtube.com/attribution_link?a=_NDwn20sMog&u=/watch?v=zl-GC16vBm4&feature=share"
  [1]=>
  string(11) "zl-GC16vBm4"
  [2]=>
  string(14) "&feature=share"
}