Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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:preg_match_所有来自文本的Youtube视频ID_Php_Regex_Preg Match_Preg Match All - Fatal编程技术网

PHP:preg_match_所有来自文本的Youtube视频ID

PHP:preg_match_所有来自文本的Youtube视频ID,php,regex,preg-match,preg-match-all,Php,Regex,Preg Match,Preg Match All,我想从文本youtube url字符串中提取,如https://www.youtube.com/watch?time_continue=218&v=0EB7zh_7UE4和视频id类似于0EB7zh_7UE4,因此我可以根据视频id在字符串后面插入文本。这是我的示例文本: This is an example page will show up https://www.youtube.com/watch?time_continue=218&v=0EB7zh_7UE4 Bike https

我想从文本youtube url字符串中提取,如
https://www.youtube.com/watch?time_continue=218&v=0EB7zh_7UE4
和视频id类似于
0EB7zh_7UE4
,因此我可以根据视频id在字符串后面插入文本。这是我的示例文本:

This is an example page will show up https://www.youtube.com/watch?time_continue=218&v=0EB7zh_7UE4 Bike https://www.youtube.com/watch?v=0EB7zh_7UE4&feature=youtu.be&app=desktop messenger by day, aspiring actor by night, and this is my website. I live in https://youtu.be/1EB7zh_7UE4 Los Angeles, have a great dog named Jack, and I https://www.youtube.com/watch?v=0EB7zh_7UE4&feature=youtu.be like piña coladasdoohickeys https://www.youtube.com/watch?v=4EB7zh_7UE4 you should go to <a href="http://example.com/wp-admin/">your dashboard</a> to delete this page and create new pages for your content. Have fun!

https://www.youtube.com/watch?v=0EB7zh_7UE4

more

https://www.youtube.com/watch?v=2EB7zh_7UE4&feature=youtu.be

That\'s all..
我错在哪里


注意:问题+示例文本已更新。

要展开我的评论,您每次都将结果文本替换为原始字符串$sample\u text。这是一个简单的修复方法,只需在开始时初始化$processed_文本,然后进行操作

function regex($sample_text) {
    $processed_text = $sample_text;
    if (preg_match_all('#(?:https?:\/\/)?(?:m\.|www\.)?(?:youtu\.be\/|youtube\-nocookie\.com\/embed\/|youtube\.com\/(?:embed\/|v\/|e\/|\?v=|shared\?ci=|watch\?v=|watch\?.+&v=))([-_A-Za-z0-9]{10}[AEIMQUYcgkosw048])(.*?)\b#s', $sample_text, $matches, PREG_SET_ORDER)) {
        print_r($matches);
        foreach ($matches as $match) {
            $add = ' (here)';
            $processed_text = str_replace($match[0], $match[0] . $add, $processed_text);
        }
    }
    return $processed_text;
}
echo regex($sample_test);
您的正则表达式也与URL结尾不匹配。对于您提供的示例文本,您可以匹配任何非空白的内容:

'#(?:https?:\/\/)?(?:m\.|www\.)?(?:youtu\.be\/|youtube\-nocookie\.com\/embed\/|youtube\.com\/(?:embed\/|v\/|e\/|\?v=|shared\?ci=|watch\?v=|watch\?.+&v=))([-_A-Za-z0-9]{10}[AEIMQUYcgkosw048])\S*#s'
但是,这与
等字符不匹配,但您可以将这些字符作为
|
添加到组中。您似乎对正则表达式有很好的理解,因此我假设您可以解决此问题-如果没有,请发表评论,我将更新我的答案


为了完整起见,我已将完整的代码包含在我的正则表达式中:

function regex($sample_text) {
    $processed_text = $sample_text;
    if (preg_match_all('#(?:https?:\/\/)?(?:m\.|www\.)?(?:youtu\.be\/|youtube\-nocookie\.com\/embed\/|youtube\.com\/(?:embed\/|v\/|e\/|\?v=|shared\?ci=|watch\?v=|watch\?.+&v=))([-_A-Za-z0-9]{10}[AEIMQUYcgkosw048])\S*#s', $sample_text, $matches, PREG_SET_ORDER)) {
        print_r($matches);
        foreach ($matches as $match) {
            $add = ' (here)';
            $processed_text = str_replace($match[0], $match[0] . $add, $processed_text);
        }
    }
    return $processed_text;
}
echo regex($sample_test);
不过,我在网上找到了以下解决方案

preg_match_all('/(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})\W/', $str, $match);
print_r($match);
你可以用

https?://\S+?\Qyoutube.com\E\S+?v=\K[^&\s]+

请参阅。

仅作记录,我最终使用了此“简单”函数,其基础是:


这就是我一直在做的事情:

function FindYouTubeId($url)
{
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
$youtube_id = $match[1];
return $youtube_id;
}

“它复制ID值”是什么意思?预期的输出是什么,以及您看到的输出是什么?@Syscall这不是通用代码,而是为stackoberflow.com编辑的。我错过了它。问题已得到解决。$processed\u text每次都从$sample\u文本中重置,而不是每个值的运行替换。@IMSoP我想插入“”(此处)“在YouTube URL链接字符串的末尾,而不是将它添加到中间。”这是错误的一部分。这就是为什么我返回Mulle注入。如此明亮!谢谢。对不起,我将不再编辑这个问题:-我的主要问题是正则表达式。您的建议仍然不匹配所有字符串,特别是当字符串以新的Li结尾时。neSorry,我从正则表达式中漏掉了一个
*
。编辑了我的答案-这与行尾匹配,因为您提供了
s
开关(仅在单行上匹配)使用我提供的代码修复
$processed_text
问题,然后用我提供的正则表达式替换代码中的正则表达式。我之所以选择此答案,是因为我最符合我的原始意图。谢谢大家!你们做得很脏,但很管用!如果有人发布更好的解决方案,将保留问题一段时间,但你们是现在最好的。
preg_match_all('/(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})\W/', $str, $match);
print_r($match);
https?://\S+?\Qyoutube.com\E\S+?v=\K[^&\s]+
function filter($content) {
return preg_replace_callback('#(?:https?:\/\/)?(?:m\.|www\.)?(?:youtu\.be\/|youtube\-nocookie\.com\/embed\/|youtube\.com\/(?:embed\/|v\/|e\/|\?v=|shared\?ci=|watch\?v=|watch\?.+&v=))([-_A-Za-z0-9]{10}[AEIMQUYcgkosw048])\S*#s', function($match) {
    return sprintf('%s my replace with 2nd parameter found %s', $match[0], $match[1]);
}, $content);    
}
function FindYouTubeId($url)
{
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
$youtube_id = $match[1];
return $youtube_id;
}