Jump to content

[SOLVED] Open File, Remove Everything But Object, Overwrite File


fanfavorite

Recommended Posts

Hi All,

 

I am looking to open a file, read the contents of that file, remove everything except the <object>Data</object> tag and then overwrite the file that is currently open.  Now the object tag has elements in it like <object classid="blah"> and so on.  Object also is all uppercase, all lowercase and so on:

 

	$dir = "/path/to/my/file.html";

$f = fopen($dir, "r+");
$listing = fread($f, filesize($dir));
preg_match_all("%<object>(.*?)</object>%" , $listing, $listing2);
if (fwrite($f, $listing2) == FALSE) {
	echo "Cannot write to file";
}
fclose($f);

 

Now I am not sure to overwrite everything and still be able to read the entire file, if I have to open it twice or not.  I know w+ will open the file and make it 0 length, so I assume that means that I cannot read the current data that was in there before.  As for the preg_match_all, I am always lost with this regex stuff, so any help is appreciated.  Thanks! 

Link to comment
Share on other sites

Here is the code I am trying to get information out of:

 

<HTML>
<HEAD>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<TITLE>013</TITLE>
</HEAD>
<body bgcolor="#ffffff" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<center>
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
WIDTH="800" HEIGHT="267" id="013" ALIGN="">
<PARAM NAME=movie VALUE="013.swf"> <param name="menu" value="false">
<PARAM NAME=menu VALUE="false"><PARAM NAME=bgcolor VALUE=#FFFFFF> <EMBED src="013.swf" quality=high bgcolor=#FFFFFF  WIDTH="800" HEIGHT="267" NAME="013" ALIGN=""
TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>
</OBJECT>
</center>
</BODY>
</HTML>

 

Ideally I would like to extract the width, height, src, and version from the above and make my own code with it.  If someone could point me in the right direction to do one of them, then I could probably do the rest.  I need to make sure that it uses /i for case insensitivity. 

 

Thanks. 

Link to comment
Share on other sites

Ok I tried this:

 

preg_match_all('~(?<=WIDTH=)\D?(\d+)~',$listing, $matches);

preg_match_all('~(?<=HEIGHT=)\D?(\d+)~',$listing, $matches2);

preg_match_all('~(?<=SRC=)\D?(\d+)~',$listing, $matches3);

preg_match_all('~(?=version=)\D?(\d+)~',$listing, $matches4);

echo "Width: ".$matches[1][1];

echo " Height: ".$matches2[1][1];

echo " SRC: ".$matches3[1][1];

echo " Version: ".$matches4[1][1];

 

The WIDTH AND HEIGHT work, but I need to make it case insensitive.  I tried putting /i in many places, but it didn't work.  The other two, I am not sure how to modify.   

Link to comment
Share on other sites

Ok I am still playing but I changed it to:

 

preg_match_all("/ width=[\"']?(.*)[\"']?[ >]/Uis",$listing, $matches);

preg_match_all("/ height=[\"']?(.*)[\"']?[ >]/Uis",$listing, $matches2);

preg_match_all("/ src=[\"']?(.*)[\"']?[ >]/Uis",$listing, $matches3);

preg_match_all("/version=[\"']?(.*)[\"']?[ >]/Uis",$listing, $matches4);

echo "Width: ".$matches[0][0];

echo " Height: ".$matches2[0][0];

echo " SRC: ".$matches3[0][0];

echo " Version: ".$matches4[0][0];

 

This displays:

 

Width: WIDTH="800" Height: HEIGHT="267" SRC: src="013.swf" Version: version=6,0,0,0". 

 

Is there a way to revise this to exclude everything except the actual value between? 

 

 

Link to comment
Share on other sites

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.