doujing5150 2017-01-05 19:14
浏览 74
已采纳

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

I want to remove all references to autoplay in an URL - even multiple times if they exist - for all videos except the one (Uj1ykZWtPYI). The other settings URL parameters should remain.

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>

It appends autoplay=0 programmatically.


For the specified video (Uj1ykZWtPYI), it should behave like this:

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>`

It appends autoplay=1 programmatically.


What I've tried so far in 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条回答 默认 最新

  • duandan4680 2017-01-05 22:49
    关注

    Matching all links without Uj1ykZWtPYI

    You can search for this regular expression to find all matches without Uj1ykZWtPYI in the URL:

    \b(src="(?:(?!Uj1ykZWtPYI|").)+?)(?:&autoplay=(?:1|0))([^"]*?)"|\b(src="(?:(?!Uj1ykZWtPYI|"|&autoplay=(?:1|0)).)+?)"
    

    Then, replace the it with this (autoplay is zero):

    $1$2$3&autoplay=0"
    

    Explanation:

    • \b(src="(?:(?!Uj1ykZWtPYI|").)+?)(?:&autoplay=(?:1|0))([^"]*?)": The first part of the pattern looks for any characters after src=", which are not equal an apostrophe [^"] or !Uj1ykZWtPYI and stops at autoplay. This forms the first group. The pattern has to have the characters &autoplay=1 or &autoplay=0 in it. After autoplay, everything except the " character is included into the second group - until ".
    • \b(src="(?:(?!Uj1ykZWtPYI|"|&autoplay=(?:1|0)).)+?)": The second part matches any url without an autoplay, a " and Uj1ykZWtPYI in it, but otherwise is the same as the first pattern.
    • If pattern 1 matches, the groups $1 and $2 form the valid URL without autosave. If it does not match, but the second one does, $3 will contain the full URL. So, $1$2$3 depicts in any of the two cases the full URL. &autoplay=0 is then added to the full URL afterwards.

    This pattern only works, if autoplay is not the first argument (?autoplay).


    Matching all links including the video code Uj1ykZWtPYI

    If you want to match every link with Uj1ykZWtPYI in it to add autoplay=1 you can use a pretty similar pattern:

    \b(src="[^"]*?Uj1ykZWtPYI[^"]*?)(?:&autoplay=(?:1|0))([^"]*?)"|\b(src="[^"]*?Uj1ykZWtPYI(?:(?!&autoplay=(?:1|0))[^"])+?)"
    

    Then replace it with this (autoplay is one):

    $1$2$3&autoplay=1"
    

    Live Example

    Here you can see both patterns in action (JavaScript) to replace your example string (all four example string combinations are added):

    // 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));

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序