fanfavorite Posted July 4, 2008 Share Posted July 4, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/113234-solved-open-file-remove-everything-but-object-overwrite-file/ Share on other sites More sharing options...
fanfavorite Posted July 4, 2008 Author Share Posted July 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/113234-solved-open-file-remove-everything-but-object-overwrite-file/#findComment-581789 Share on other sites More sharing options...
fanfavorite Posted July 4, 2008 Author Share Posted July 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/113234-solved-open-file-remove-everything-but-object-overwrite-file/#findComment-581835 Share on other sites More sharing options...
fanfavorite Posted July 4, 2008 Author Share Posted July 4, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/113234-solved-open-file-remove-everything-but-object-overwrite-file/#findComment-581847 Share on other sites More sharing options...
corbin Posted July 4, 2008 Share Posted July 4, 2008 Try print_r()'ing matches, matches2, matches3, and matches4, and you should see what you need in there some where ;p. Quote Link to comment https://forums.phpfreaks.com/topic/113234-solved-open-file-remove-everything-but-object-overwrite-file/#findComment-581899 Share on other sites More sharing options...
fanfavorite Posted July 4, 2008 Author Share Posted July 4, 2008 Thanks! I was doing that, but guess I was looking at code too long today lol. Never saw that before. Now I see that it records both with the Width and without. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/113234-solved-open-file-remove-everything-but-object-overwrite-file/#findComment-581909 Share on other sites More sharing options...
corbin Posted July 4, 2008 Share Posted July 4, 2008 No problem ;p. Quote Link to comment https://forums.phpfreaks.com/topic/113234-solved-open-file-remove-everything-but-object-overwrite-file/#findComment-581919 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.