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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。