search and replace in php string

2 posts by 2 authors in: Forums > CMS Builder
Last Post: July 29, 2011   (RSS)

By zaba - July 29, 2011

Hi not really cms builder question, more of a php thing.

I have a field in my database "video_ref" that the client can put embed information for videos either from youtube or other sources.

The video will appear in 2 different places at 2 different sizes. The video dimensions in the input code will vary.

What I want is to be able to search the following or similar code looking for all instances of width="whatever" and height="whatever" and replace them with specific dimensions. this I can perform on the different pages requiring different sizes.

typical code returned for <?php echo $vidrecord['video_ref'] ?>would be:
<object id="flashObj" width="294" height="180" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,47,0"><param name="movie" value="http://c.brightcove.com/services/viewer/federated_f9?isVid=1" /><param name="bgcolor" value="#FFFFFF" /><param name="flashVars" value="videoId=1085261241001&playerID=64461390001&playerKey=AQ~~,AAAADvh-Hkk~,yxKZp3GImt74tvNzevDAR8IIfqlagIoN&domain=embed&dynamicStreaming=true" /><param name="base" value="http://admin.brightcove.com" /><param name="seamlesstabbing" value="false" /><param name="allowFullScreen" value="true" /><param name="swLiveConnect" value="true" /><param name="allowScriptAccess" value="always" /><embed src="http://c.brightcove.com/services/viewer/federated_f9?isVid=1" bgcolor="#FFFFFF" flashVars="videoId=1085261241001&playerID=64461390001&playerKey=AQ~~,AAAADvh-Hkk~,yxKZp3GImt74tvNzevDAR8IIfqlagIoN&domain=embed&dynamicStreaming=true" base="http://admin.brightcove.com" name="flashObj" width="294" height="180" seamlesstabbing="false" type="application/x-shockwave-flash" allowFullScreen="true" swLiveConnect="true" allowScriptAccess="always" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>

hope this makes sense. any php help would be appreciated!

Re: [zaba] search and replace in php string

By Jason - July 29, 2011

Hi,

You could try something like this:

<?php
$heightPattern = "/height=\"[0-9]*\"/";
$widthPattern = "/width=\"[0-9]*\"/";

$vidrecord['video_ref'] = preg_replace($heightPattern, "height='120'", $vidrecord['video_ref']);
$vidrecord['video_ref'] = preg_replace($widthPattern, "width='200'", $vidrecord['video_ref']);
?>


You would then replace "height='120'" and "width=''200'" with what ever values you want.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/