youtu(\.be|be\.com)/(?:watch?v=)(a-zA-Z0-9_-]{11}(?:&.*)(#t=[0-9hms:]+)
Nope, that code won't work without a few adjustments.
Easiest way I have to test things is
RegExr, and even then it has a bunch of technicalities to it.
'\[youtube\](?:https://(?:www\.)?youtu(?:\.be/|be\.com/watch\?v=))?([\w-]{11})(?:(?:\?|\&)t=\w{1,})?\[/youtube\]'si
I know there might be more escaped characters than needed, it's just that normally either RegExr or JS (or both) will complain if you don't escape a few extra characters, even though they seem valid in PHP. This will catch either the YouTube video URL, the YouTube share link or the video ID and supports the t=4s variable too. I didn't bother to make it check specifically for the right syntax, so if someone wrote t=potato it will accept it, but it won't accept it if someone tries to write t=4s&autoplay=1 because it'll look at the & and "autoplay=" and realize that it isn't equal to "t=" and reject it (you can't even put multiple "t=" because it only looks for one).
Originally posted by Xkeeper
Technically, supporting "width=" and "height=" in the youtube tag could be done, too. There are lots of options.
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA. That is all.
More variables = higher chances of me derping up something, and hell if I know how to implement both height and width in BBcode and backreference it properly. You also need to have a minimum/maximum height/width too to make sure it doesn't get abused. It
can be done, though. I guess [youtube width="600" height="400"]https://www.youtube.com/watch?v=FZBnxoKl3tY[/youtube] could work.
'\[youtube( width=\d{3})?( height=\d{3})?\](?:https://(?:www\.)?youtu(?:\.be/|be\.com/watch\?v=))?([\w-]{11})(?:(?:\?|\&)t=\w{1,})?\[/youtube\]'si
You cannot put them in reverse order because derp (width first, then height). Minimum width/height would be 100px, maximum would be 999px. Feel free to suggest lower/higher values, reminder that regexs only check the amount of digits not the values themselves so I can only check if the amount of digits is n or higher/lower (or exactly n). There's no escaping this one, the backreference will need to be changed to \3 and there needs to be default values added to some prior variable in the case height and width are not specified.
Second regex value (the output) would be:
'<iframe src="https://www.youtube.com/embed/\3"\1\2 frameborder="0" allowfullscreen="allowfullscreen"></iframe>'
In this case, there needs to be something that will specify a value for \1 and \2 in case there's none. I would know how to do this in JavaScript (store in variable previously to replacement, check if variable is valid, if it isn't then generate a default one, insert it on the full regex code, replace). Something like:
var msg = "[youtube height=999 width=500]"; //This is the post message to be decoded.
widthVar = msg.match(/width=\d{3}/i)
heightVar = msg.match(/height=\d{3}/i)
if (!widthVar) {widthVar = " width=560";}
if (!heightVar) {heightVar = " width=315";}
But in PHP instead of JS. Then it would just be matter of calling those values into the regex instead of \1 and \2. I guess if we're going this route somehow then you could also check the values to see if they're <minimum or >maximum properly instead of relying on regexs.
I'm making myself confused.

Is this complicated enough? Should I make it more complicated?
____________________