问题描述
我创建了一个函数,可以为 youtube 嵌入代码设置高度、宽度和 wmode=transparent.现在 youtube 正在返回 iframe 代码.所以,我需要在 youtube src 的末尾附加?wmode=transparent".
I have created a function which will set height, width and wmode=transparent for youtube embed codes. Now youtube is returning iframe code. So, I need to append "?wmode=transparent" at the end of youtube src.
例如
原始代码:
For eg.
Original code :
<iframe title="YouTube 视频播放器" width="420" height="349" src="http://www.youtube.com/embed/aXNUL1-urg8" frameborder="0" allowfullscreen=""></iframe>
我希望它被替换为:
<iframe title="YouTube 视频播放器" width=" mywidth" height=" myheight" src="http://www.youtube.com/嵌入/aXNUL1-urg8 ?wmode=transparent" frameborder="0" allowfullscreen=""></iframe>
替换高度和宽度有效,但替换 src 不起作用.
Replacing height and width is working, but replacing src does not work.
我正在使用下面的正则表达式
I am using below regex
$patterns[] = '/src="http:\/\/www.youtube.com\/embed\/[a-zA-Z0-9._-]"/';
$replacements[] = 'src="http://www.youtube.com/embed/${1}?wmode=transparent"';
$patterns[] = '/src="http:\/\/www.youtube.com\/embed\/[a-zA-Z0-9._-]"/';
$replacements[] = 'src="http://www.youtube.com/embed/${1}?wmode=transparent"';
下面是我的函数.
函数 SetHeightWidthVideo($markup,$w='200',$h='120'){//使其wmode =透明
$markup = str_replace('<embed ','<embed wmode="transparent" ',$markup);
//$w = '200';
//$h = '120';
$patterns = array();
$replacements = array();
if( !empty($w) )
{
$patterns[] = '/width="([0-9]+)"/';
$patterns[] = '/width:([0-9]+)/';
$replacements[] = 'width="'.$w.'"';
$replacements[] = 'width:'.$w;
}
if( !empty($h) )
{
$patterns[] = '/height="([0-9]+)"/';
$patterns[] = '/height:([0-9]+)/';
$replacements[] = 'height="'.$h.'"';
$replacements[] = 'height:'.$h;
}
$patterns[] = '/src="http:\/\/www\.youtube\.com\/embed\/[a-zA-Z0-9._-]"/';
$replacements[] = 'src="http://www.youtube.com/embed/${1}?wmode=transparent"';
return preg_replace($patterns, $replacements, $markup);
}
请帮忙.提前致谢.
推荐答案
尝试:
$patterns[] = '/src="(.*?)"/';
$replacements[] = 'src="${1}?wmode=transparent"';
return preg_replace($patterns, $replacements, $markup);
这篇关于php preg_replace 帮助 iframe src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!