drdu53813 2014-03-22 17:07
浏览 89
已采纳

preg_replace没有在引用中转义“?”

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)

  • 写回答

2条回答 默认 最新

  • dqpfkzu360216 2014-03-23 18:10
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化