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.

<iframe src="//" frameborder="0" allowfullscreen=""></iframe>

<iframe src="//" frameborder="0" allowfullscreen=""></iframe>

It appends autoplay=0 programmatically.

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

<iframe src="//" frameborder="0" allowfullscreen=""></iframe>

<iframe src="//" 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('/\/embed\/([^Uj1ykZWtPYI]*)([^"&]*)/', '$1$2&autoplay=0', $content['message']);

// Result
// <iframe src="//" frameborder="0" allowfullscreen="">

// Matching specific video
$content['message'] = preg_replace('/\/embed\/([Uj1ykZWtPYI]*)([^"&]*)/', '$1$2&autoplay=1', $content['message']);

// Result
// <iframe src="//" 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:


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



    • \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:


    Then replace it with this (autoplay is one):


    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="//" frameborder="0" allowfullscreen=""></iframe>';
    // Uj1ykZWtPYI as code, including autoplay
    var string2 = '<iframe src="//" frameborder="0" allowfullscreen=""></iframe>';
    // 1337 as code, autoplay not included
    var string3 = '<iframe src="//" frameborder="0" allowfullscreen=""></iframe>';
    // Uj1ykZWtPYIas code, autoplay not included
    var string4 = '<iframe src="//" 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));

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥66 定制开发肯德基自动化网站下单软件
  • ¥20 vscode虚拟环境依赖包未安装
  • ¥15 odoo17关于owl开发js代码问题
  • ¥15 光纤中多普勒频移公式的推导
  • ¥15 怎么制作一个人脸识别门禁系统
  • ¥20 大华dss监控平台网络关闭登不进去
  • ¥15 请使用蚁群算法解决下列问题,并给出我完整的代码
  • ¥20 关于php录入完成后,批量更新数据库
  • ¥15 请教往复密封润滑问题
  • ¥15 cocos creator发布ios包