obay Posted November 24, 2008 Share Posted November 24, 2008 Waah! Help... Using php and regular expressions, is it possible to get the filename of a file in a code such as this: <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="400" height="300"><param name="src" value="../bcms_uploads/myVideo.flv" /><param name="width" value="400" /><param name="height" value="300" /><embed type="application/x-shockwave-flash" src="../bcms_uploads/myVideo.flv" width="400" height="300"></embed></object> and turn it into this: <a href="../bcms_uploads/myVideo.flv" style="display:block;width:400px;height:300px" id="player"> </a> Note that of the first code, only the filename was "extracted" and insert into the second code. Is this possible? If so, how do I do it in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/134007-solved-regex-question-is-this-possible/ Share on other sites More sharing options...
ddrudik Posted November 24, 2008 Share Posted November 24, 2008 You could also incorporate the height and width params into the regex, but with only your initial requirement: $html=preg_replace('~<object\s[^>]*/swflash.cab[^>]*><param\s[^>]*value=("[^"]*")(??!</object>).)*</object>~is','<a href=$1 style="display:block;width:400px;height:300px" id="player"></a>',$html); Quote Link to comment https://forums.phpfreaks.com/topic/134007-solved-regex-question-is-this-possible/#findComment-697551 Share on other sites More sharing options...
obay Posted December 5, 2008 Author Share Posted December 5, 2008 wow! this is great! however, there might be some typo... the result gives me: <a href="400" style="display:block;width:400px;height:300px" id="player"></a> rather than <a href="../bcms_uploads/myVideo.flv" style="display:block;width:400px;height:300px" id="player"></a> that is, the href is incorrect could you help me with this? i tried changing it to $0 or $2, it doesn't work thanks! Quote Link to comment https://forums.phpfreaks.com/topic/134007-solved-regex-question-is-this-possible/#findComment-706546 Share on other sites More sharing options...
premiso Posted December 5, 2008 Share Posted December 5, 2008 $string = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="400" height="300"><param name="src" value="../bcms_uploads/myVideo.flv" /><param name="width" value="400" /><param name="height" value="300" /><embed type="application/x-shockwave-flash" src="../bcms_uploads/myVideo.flv" width="400" height="300"></embed></object>'; $html=preg_replace('~<object\s[^>]*/swflash.cab[^>]*><param name="src"\s[^>]*value=("[^"]*")(??!</object>).)*</object>~is','<a href=$1 style="display:block;width:400px;height:300px" id="player"></a>',$string); Should get you the correct results. Quote Link to comment https://forums.phpfreaks.com/topic/134007-solved-regex-question-is-this-possible/#findComment-706927 Share on other sites More sharing options...
premiso Posted December 5, 2008 Share Posted December 5, 2008 I got really bored and decided to shorten the regex =) <?php $string = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="400" height="300"><param name="src" value="../bcms_uploads/myVideo.flv" /><param name="width" value="400" /><param name="height" value="300" /><embed type="application/x-shockwave-flash" src="../bcms_uploads/myVideo.flv" width="400" height="300"></embed></object>'; $html=preg_replace('~<object.*value="(.*)(\.\w{3})(" />)(.*)</object>~','<a href="$1$2" style="display:block;width:400px;height:300px" id="player"></a>',$string); echo $html . " HTML"; ?> Found another way that works which may be more dynamic (not requiring a param of src) $html=preg_replace('~<object.*src="(.*)(\.\w{3})(")(.*)</object>~','<a href="$1$2" style="display:block;width:400px;height:300px" id="player">df</a>',$string); A bit shorter and should get you desired results too =) (I wanted to learn a bit more about regex so yea) Quote Link to comment https://forums.phpfreaks.com/topic/134007-solved-regex-question-is-this-possible/#findComment-707026 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.