正则表达式Youtube - 除了特定的视频哈希外,将自动播放设置为0

时间:2017-01-05 19:14:40

标签: php regex youtube

我想删除网址中对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>

1 个答案:

答案 0 :(得分:1)

匹配没有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的网址,但其他方面与第一个模式相同。
  • 如果模式1匹配,则组$1$2形成没有autosave的有效网址。如果它不匹配,但第二个匹配,$3将包含完整的URL。因此,$1$2$3在两种情况下都描述了完整的URL。然后,&autoplay=0会添加到完整的网址中。

只有当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));