【发布时间】:2011-07-21 20:13:24
【问题描述】:
我创建了一个函数,它将为 youtube 嵌入代码设置高度、宽度和 wmode=transparent。现在 youtube 正在返回 iframe 代码。所以,我需要在 youtube src 的末尾附加“?wmode=transparent”。
例如。
原代码:
我希望将其替换为:
替换高度和宽度是有效的,但替换 src 不起作用。
我正在使用下面的正则表达式
$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);}
请帮忙。提前致谢。
【问题讨论】:
-
您需要在
[a-zA-Z0-9._-]表达式的末尾添加一个+。现在它只能匹配一个字符。
标签: php regex preg-replace