我有一个小 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>
知道为什么会发生这种情况吗?
试试这个:
$video_code = preg_replace ('/(width=[\'"])(.*?)([\'"])/', '$1620$3', $video_code);
$video_code = preg_replace ('/(height=[\'"])(.*?)([\'"])/', '$1' . $height . '$3', $video_code);
转义了一些字符,并且您要替换
()
之间的所有内容,因此替换模式中的文本 height=
是不必要的。
$video_code = preg_replace('/width=[\'"][^\'"]+[\'"]/', 'width="'.$width.'"', $video_code);
$video_code = preg_replace('/height=[\'"][^\'"]+[\'"]/', 'height="'.$height.'"', $video_code);