preg_replace不起作用

时间:2012-05-25 13:49:32

标签: php preg-replace

我的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>

知道为什么会这样吗?

由于

乔治

3 个答案:

答案 0 :(得分:1)

试试这个:

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

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

将某些字符撤消并替换()之间的所有字符,因此替换模式中的文字height=是不必要的。

答案 1 :(得分:1)

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

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

希望这有帮助。

答案 2 :(得分:1)

$video_code = preg_replace('/width=[\'"][^\'"]+[\'"]/', 'width="'.$width.'"', $video_code);
$video_code = preg_replace('/height=[\'"][^\'"]+[\'"]/', 'height="'.$height.'"', $video_code);
相关问题