phphtmliframereplacehtml-parsing

Change attribute values of <iframe> tags


I've small preg_replace problem. I need to resize iframe code output so I'm using preg_replace, regex seems to work fine, but result is same as input no changes. Here code I'm using:

$video_code = preg_replace( '/width="(.*?)"/', 'width="'.$width.'"',  $video_code );
$video_code = preg_replace( '/height="(.*?)"/', 'height="'.$height.'"',  $video_code );

This iframe that $video_code contains:

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

Any idea why is this happening?


Solution

  • Try this:

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

    Escaped some characters and you're replacing everything inbetween the (), so the text height= in the replacement pattern is unnecessary.