我的preg_replace问题很小。我需要调整iframe代码输出的大小,所以我使用preg_replace,正则表达式似乎工作正常,但结果与输入相同没有变化。这里是我正在使用的代码:
$video_code = preg_replace( '/width="(.*?)"/', 'width="'.$width.'"', $video_code );
$video_code = preg_replace( '/height="(.*?)"/', 'height="'.$height.'"', $video_code );
$video_code
包含的iframe:
<iframe class='sproutvideo-player' type='text/html' src='http://videos.sproutvideo.com/embed/189bd8b4191ee1c390/d0dc5859e1d409ed?type=hd®ularColors0=666565®ularColors1=595756&hoverColors0=ed2409&hoverColors1=d1390f&highlightColors1=c9c3c9&noBigPlay=true' width='768' height='432' frameborder='0'></iframe>
知道为什么会这样吗?
由于
乔治
答案 0 :(得分:1)
试试这个:
$video_code = preg_replace ('/(width=[\'"])(.*?)([\'"])/', '$1620$3', $video_code);
$video_code = preg_replace ('/(height=[\'"])(.*?)([\'"])/', '$1' . $height . '$3', $video_code);
将某些字符撤消并替换()
之间的所有字符,因此替换模式中的文字height=
是不必要的。
答案 1 :(得分:1)
答案 2 :(得分:1)
$video_code = preg_replace('/width=[\'"][^\'"]+[\'"]/', 'width="'.$width.'"', $video_code);
$video_code = preg_replace('/height=[\'"][^\'"]+[\'"]/', 'height="'.$height.'"', $video_code);