更改<iframe>标签的属性值

问题描述 投票:0回答:3

我有一个小 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&regularColors0=666565&regularColors1=595756&hoverColors0=ed2409&hoverColors1=d1390f&highlightColors1=c9c3c9&noBigPlay=true' width='768' height='432' frameborder='0'></iframe>

知道为什么会发生这种情况吗?

php html iframe replace html-parsing
3个回答
1
投票

试试这个:

$video_code = preg_replace ('/(width=[\'"])(.*?)([\'"])/', '$1620$3',  $video_code);

$video_code = preg_replace ('/(height=[\'"])(.*?)([\'"])/', '$1' . $height . '$3',  $video_code);

转义了一些字符,并且您要替换

()
之间的所有内容,因此替换模式中的文本
height=
是不必要的。


1
投票

我使用了你的代码,它对我来说效果很好。 (做了一些小改动,但不多)

这是我使用的代码: http://codepad.viper-7.com/fafGYW

希望这有帮助。


1
投票
$video_code = preg_replace('/width=[\'"][^\'"]+[\'"]/', 'width="'.$width.'"', $video_code);
$video_code = preg_replace('/height=[\'"][^\'"]+[\'"]/', 'height="'.$height.'"', $video_code);
© www.soinside.com 2019 - 2024. All rights reserved.