首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Regex Youtube -将自动播放设置为0,但特定视频哈希除外

Regex Youtube -将自动播放设置为0,但特定视频哈希除外
EN

Stack Overflow用户
提问于 2017-01-05 19:14:39
回答 1查看 242关注 0票数 0

我想删除URL中所有对autoplay的引用--如果它们存在的话,甚至多次--对于所有的视频,除了一个(Uj1ykZWtPYI)。其他设置URL参数应保持不变。

代码语言:javascript
复制
Source: 
<iframe src="//www.youtube.com/embed/1337?wmode=transparent&autoplay=1&controls=0&showinfo=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe>

Desired:
<iframe src="//www.youtube.com/embed/1337?wmode=transparent&controls=0&showinfo=0&modestbranding=1&autoplay=0" frameborder="0" allowfullscreen=""></iframe>

它以编程方式附加autoplay=0

对于指定的视频(Uj1ykZWtPYI),它的行为应该如下所示:

代码语言:javascript
复制
Source: 
<iframe src="//www.youtube.com/embed/Uj1ykZWtPYI?wmode=transparent&autoplay=0&controls=0&showinfo=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe>

Desired:
<iframe src="//www.youtube.com/embed/Uj1ykZWtPYI?wmode=transparent&controls=0&showinfo=0&modestbranding=1&autoplay=1" frameborder="0" allowfullscreen=""></iframe>`

它以编程方式附加autoplay=1

到目前为止,我在PHP中尝试过的是:

代码语言:javascript
复制
// Non-matching specific video
$content['message'] = preg_replace('/youtube.com\/embed\/([^Uj1ykZWtPYI]*)([^"&]*)/', 'youtube.com/embed/$1$2&autoplay=0', $content['message']);

// Result
// <iframe src="//www.youtube.com/embed/W6hr-o6JiWs?wmode=transparent&autoplay=1&autoplay=0" frameborder="0" allowfullscreen="">

// Matching specific video
$content['message'] = preg_replace('/youtube.com\/embed\/([Uj1ykZWtPYI]*)([^"&]*)/', 'youtube.com/embed/$1$2&autoplay=1', $content['message']);

// Result
// <iframe src="//www.youtube.com/embed/Uj1ykZWtPYI?wmode=transparent&autoplay=1&autoplay=0&controls=0&showinfo=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-05 22:49:18

匹配所有没有Uj1ykZWtPYI的链接

您可以搜索这个正则表达式以查找URL中没有Uj1ykZWtPYI的所有匹配:

代码语言:javascript
复制
\b(src="(?:(?!Uj1ykZWtPYI|").)+?)(?:&autoplay=(?:1|0))([^"]*?)"|\b(src="(?:(?!Uj1ykZWtPYI|"|&autoplay=(?:1|0)).)+?)"

然后,将it替换为以下内容(自动播放为0):

代码语言:javascript
复制
$1$2$3&autoplay=0"

解释:

  • \b(src="(?:(?!Uj1ykZWtPYI|").)+?)(?:&autoplay=(?:1|0))([^"]*?)":模式的第一部分查找在src="之后的任何字符,这些字符不等于撇号[^"]!Uj1ykZWtPYI,停止于自动播放。这是第一组。模式中必须包含&autoplay=1&autoplay=0字符。在自动播放之后,除"字符之外的所有内容都包含在第二组中--直到"
  • \b(src="(?:(?!Uj1ykZWtPYI|"|&autoplay=(?:1|0)).)+?)":第二部分匹配没有autoplay"Uj1ykZWtPYI的任何url,但其他部分与第一种模式相同。
  • 如果模式1匹配,那么组$1$2将形成没有autosave的有效URL。如果它不匹配,但第二个匹配,$3将包含完整的网址。因此,$1$2$3在这两种情况下都描述了完整的URL。然后将&autoplay=0添加到完整的URL中。

如果自动播放不是第一个参数(?autoplay).,则此模式只起作用。

匹配所有链接,包括视频代码Uj1ykZWtPYI

如果希望将每个链接与Uj1ykZWtPYI匹配起来以添加autoplay=1,则可以使用非常类似的模式:

代码语言:javascript
复制
\b(src="[^"]*?Uj1ykZWtPYI[^"]*?)(?:&autoplay=(?:1|0))([^"]*?)"|\b(src="[^"]*?Uj1ykZWtPYI(?:(?!&autoplay=(?:1|0))[^"])+?)"

然后用以下内容替换它(自动播放就是其中之一):

代码语言:javascript
复制
$1$2$3&autoplay=1"

实例化

在这里,您可以看到替换示例字符串的两个模式(JavaScript) (添加了所有四个示例字符串组合):

代码语言:javascript
复制
// 1337 as code, including autoplay
var string1 = '<iframe src="//www.youtube.com/embed/1337?wmode=transparent&autoplay=1&controls=0&showinfo=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe>';
// Uj1ykZWtPYI as code, including autoplay
var string2 = '<iframe src="//www.youtube.com/embed/Uj1ykZWtPYI?wmode=transparent&autoplay=1&controls=0&showinfo=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe>';
// 1337 as code, autoplay not included
var string3 = '<iframe src="//www.youtube.com/embed/1337?wmode=transparent&controls=0&showinfo=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe>';
// Uj1ykZWtPYIas code, autoplay not included
var string4 = '<iframe src="//www.youtube.com/embed/Uj1ykZWtPYI?wmode=transparent&controls=0&showinfo=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe>';

var regex1 = /\b(src="(?:(?!Uj1ykZWtPYI|").)+?)(?:&autoplay=(?:1|0))([^"]*?)"|\b(src="(?:(?!Uj1ykZWtPYI|"|&autoplay=(?:1|0)).)+?)"/g;
var regex2 = /\b(src="[^"]*?Uj1ykZWtPYI[^"]*?)(?:&autoplay=(?:1|0))([^"]*?)"|\b(src="[^"]*?Uj1ykZWtPYI(?:(?!&autoplay=(?:1|0))[^"])+?)"/g;
var replacement1 = '$1$2$3&autoplay=0"';
var replacement2 = '$1$2$3&autoplay=1"';

console.log(string1.replace(regex1, replacement1));
console.log(string2.replace(regex2, replacement2));
console.log(string3.replace(regex1, replacement1));
console.log(string4.replace(regex2, replacement2));

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41492902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档