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>
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.$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
).
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"
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>