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 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图