我想删除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>发布于 2017-01-05 22:49:18
匹配所有没有Uj1ykZWtPYI的链接
您可以搜索这个正则表达式以查找URL中没有Uj1ykZWtPYI的所有匹配:
\b(src="(?:(?!Uj1ykZWtPYI|").)+?)(?:&autoplay=(?:1|0))([^"]*?)"|\b(src="(?:(?!Uj1ykZWtPYI|"|&autoplay=(?:1|0)).)+?)"然后,将it替换为以下内容(自动播放为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将包含完整的网址。因此,$1$2$3在这两种情况下都描述了完整的URL。然后将&autoplay=0添加到完整的URL中。如果自动播放不是第一个参数(?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));
https://stackoverflow.com/questions/41492902
复制相似问题