我想删除URL中对autoplay
的所有引用-即使其中一个存在,也要删除多次(如果存在的话)-除了一个(Uj1ykZWtPYI
)。其他设置URL参数应保留。
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
),其行为应如下所示: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中尝试过的是:
// 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>
最佳答案
匹配所有没有Uj1ykZWtPYI
的链接
您可以搜索此正则表达式以查找URL中没有Uj1ykZWtPYI
的所有匹配项:
\b(src="(?:(?!Uj1ykZWtPYI|").)+?)(?:&autoplay=(?:1|0))([^"]*?)"|\b(src="(?:(?!Uj1ykZWtPYI|"|&autoplay=(?:1|0)).)+?)"
然后,将其替换为此(自动播放为零):
$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
和$2
构成不带autosave
的有效URL。如果不匹配,但第二个匹配,则$3
将包含完整的URL。因此,$1$2$3
在两种情况下都描述了完整的URL。然后将&autoplay=0
添加到完整URL。 仅当autoplay不是第一个参数(
?autoplay
)时,此模式才有效。匹配所有链接,包括视频代码
Uj1ykZWtPYI
如果要匹配每个带有
Uj1ykZWtPYI
的链接以添加autoplay=1
,则可以使用类似的模式:\b(src="[^"]*?Uj1ykZWtPYI[^"]*?)(?:&autoplay=(?:1|0))([^"]*?)"|\b(src="[^"]*?Uj1ykZWtPYI(?:(?!&autoplay=(?:1|0))[^"])+?)"
然后将其替换为此(自动播放是其中之一):
$1$2$3&autoplay=1"
现场例子
在这里,您可以看到两种操作模式(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));
关于php - Regex Youtube-将自动播放设置为0(特定视频哈希除外),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41492902/