Jump to content

[SOLVED] regex question.. is this possible?


Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/134007-solved-regex-question-is-this-possible/
Share on other sites

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

  • 2 weeks later...

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!

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

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)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.