I have a string with lots of text, bbcodes and URLs and I extract the IDs from bbcode-Youtube-urls and then replace the bbcode with an embedded youtube iframe. so far so good, this is working.
But for my site i need to add "?wmode=opaque" to the iframe src="//www.youtube.com/embed/$1" attribute but the "?" is not working with preg_replace.
This is the basic code:
function youtube_bbcode_format($str){
// extract id
$format_search = array(
'#\[youtube\].*[?&]v=([^?&]+)\[/youtube\]#i' // Youtube extract id
);
// replace string (youtube embed iframe) plus the ?wmode=opaque parameter
$format_replace = array(
'<iframe width="320" height="180" src="//www.youtube.com/embed/$1?wmode=opaque" frameborder="0" allowfullscreen></iframe>'
);
// do the replacement
$str = preg_replace($format_search, $format_replace, $str);
return $str;
}
I have tried escaping the "?" question mark next to /embed/$1 in various ways, the following examples DON'T work: (the $1 is correctly replaced with the youtube id in all examples, I'm just not writing them down everytime)
src="//www.youtube.com/embed/$1?wmode=opaque"
In the Browser everything after $1 is missing, including the "?" result: /$1
src="//www.youtube.com/embed/$1\?wmode=opaque"
the escape is not really working. result: /$1\?wmode=opaque (backslash should be gone!)
src="//www.youtube.com/embed/$1\\?wmode=opaque"
same as before, result: /$1\?wmode=opaque
src="//www.youtube.com/embed/$1??wmode=opaque"
result: /$1??wmode=opaque
src="//www.youtube.com/embed/\${1}?wmode=opaque"
result: /${1}?wmode=opaque
The last try was the most promising because in the manual it says that's the way to handle these kind of problems, but it is not working.
Any Ideas of how to escape the "?" in the replace-string?
PS: Example for an input String:
str = "music is by [color=blue][size=20][b]Pegboard Nerds - Hero (feat. Elizaveta)[/b][/size][/color]. you can listen to it here:[br][youtube]youtube.com/watch?v=5lLclBfKj48[/youtube]";
(other bbcode tags are handled elsewhere)