Question

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)

Was it helpful?

Solution 2

Thank you all for verifying the code sample. You were all right, the code snippet itself works like expected. I was sure the problem was within the preg_replace references, but I was wrong. The origin of the problem came from another part in my source code: the $str was passed through another preg_replace function which cut the question mark out.

    preg_replace_callback("&\\b$rexProtocol$rexDomain$rexPort$rexPath$rexQuery$rexFragment(?=[?.!,;:\"]?(\s|$))&", 'callback', $str);

the preg_replace_callback function was supposed to replace text links with real links.. but if it is called after my basic code snipped, everything following the "?" (including itself) was missing.

The code snippet itself is indeed working. Thank you for helping out.

OTHER TIPS

Try this:

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'
   );

   // do the replacement
   $str = preg_replace($format_search, $format_replace, $str).'?wmode=opaque" frameborder="0" allowfullscreen></iframe>';
   return $str;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top