Zipp425 Posted August 14, 2006 Share Posted August 14, 2006 I'm currently working on a javascript, xml, html, and php, based web application.I was wondering if there was a way with php to open a file and delete a certain line and replace it. I am using PHP to create a xml file, and certain lines of that file will need to be deleted and replaced by changed data.Any help or information on how to do this would be amazing. Let me know if you need more information.Thanks!zipp425@gmail.com Quote Link to comment Share on other sites More sharing options...
simcoweb Posted August 14, 2006 Share Posted August 14, 2006 Below are the three basic ways to open a file and the corresponding character that PHP uses. * Read: 'r'Open a file for read only use. The file pointer begins at the front of the file. * Write: 'w'Open a file for write only use. In addition, the data in the file is erased and you will begin writing data at the beginning of the file. This is also called truncating a file, which we will talk about more in a later lesson. The file pointer begins at the start of the file. * Append: 'a'There are additional ways to open a file. Above we stated the standard ways to open a file. However, you can open a file in such a way that reading and writing is allowable! This combination is done by placing a plus sign "+" after the file mode character. * Read/Write: 'r+'Opens a file so that it can be read from and written to. The file pointer is at the beginning of the file. * Write/Read: 'w+'This is exactly the same as r+, except that it deletes all information in the file when the file is opened. * Append: 'a+'This is exactly the same as r+, except that the file pointer is at the end of the file.Open a file for write only use. However, the data in the file is preserved and you begin will writing data at the end of the file. The file pointer begins at the end of the file. Quote Link to comment Share on other sites More sharing options...
Zipp425 Posted August 14, 2006 Author Share Posted August 14, 2006 Ok, I knew those things. Thank you.Im wondering about deleting a specific line from a file though. Quote Link to comment Share on other sites More sharing options...
Chetan Posted August 14, 2006 Share Posted August 14, 2006 You know the line number, then use file() Quote Link to comment Share on other sites More sharing options...
Zipp425 Posted August 14, 2006 Author Share Posted August 14, 2006 sorry if this is to much to ask, but can you show me a simple example? Quote Link to comment Share on other sites More sharing options...
Chetan Posted August 14, 2006 Share Posted August 14, 2006 file.txt[code]HelloMelloTello[/CODE]file.php[code]<?php$file=file('file.txt');echo($file['1']); // Helloecho($file['2']); // Melloecho($file['3']); // Tello?>[/code] Quote Link to comment Share on other sites More sharing options...
Zipp425 Posted August 14, 2006 Author Share Posted August 14, 2006 great. thank you! Quote Link to comment Share on other sites More sharing options...
Chetan Posted August 14, 2006 Share Posted August 14, 2006 I think you know you cant append files with it, ull hav 2 code it so you get everything exept the line to append it Quote Link to comment Share on other sites More sharing options...
simcoweb Posted August 14, 2006 Share Posted August 14, 2006 In order to delete OR append a specific line there needs to be a 'key' or an identifier like a line number. [quote]$key = 'whateveryouwanttoreplace';[/quote]What file type is it? Quote Link to comment Share on other sites More sharing options...
Chetan Posted August 14, 2006 Share Posted August 14, 2006 XML as he specifiedin his post >.> Quote Link to comment Share on other sites More sharing options...
simcoweb Posted August 14, 2006 Share Posted August 14, 2006 Dohhhhhhhhhh! That's one of those 'What color was George Washington's white horse?' kinda thingsOk, question. Will the data you want to change always be in the same location in the file or will the file constantly be changing in size, lines, etc.? Quote Link to comment Share on other sites More sharing options...
Chetan Posted August 14, 2006 Share Posted August 14, 2006 No unless it is being changed somewhere else (locally or another script) Quote Link to comment Share on other sites More sharing options...
simcoweb Posted August 14, 2006 Share Posted August 14, 2006 Then you couldn't use a line number because the line number could change if the file is modified and added to. So, you'd have to use a key which basically would be the string of text. Can you post an example of what would need to be replaced periodically? Please be as specific as you can with your example including any XML tags (if applicable). Quote Link to comment Share on other sites More sharing options...
Zipp425 Posted August 14, 2006 Author Share Posted August 14, 2006 Ive made it so the same data will always be on the same line.I just need to figure out how to replace that specific line.here is the xml file that is generated[pre]<?xml version='1.0' encoding='iso-8859-1'?><data> <widgets> <profile height='600px' width='800px' borderSize='2px' borderStyle='solid' borderColor='#000000' font='Arial, Helvetica, sans-serif' fontColor='#000000' fontSize='100%' backgroundColor='#ffffff' backgroundURL='' backgroundImgAlign='' backgroundImgRepeat='' misc=''/> <textBox height='auto' width='300px' borderSize='1px' borderStyle='solid' borderColor='#000000' font='Arial, Helvetica, sans-serif' fontColor='#000000' fontSize='100%' backgroundColor='#efefef' backgroundURL='' backgroundImgAlign='' backgroundImgRepeat='' misc='' /> <friendArea height='auto' width='400px' borderSize='1px' borderStyle='solid' borderColor='#000000' font='Arial, Helvetica, sans-serif' fontColor='#000000' fontSize='100%' backgroundColor='#efefef' backgroundURL='' backgroundImgAlign='' backgroundImgRepeat='' misc='' /> </widgets></data>[/pre]so basically what i need is to select the textBox line and replace it when a user changes the values. Quote Link to comment Share on other sites More sharing options...
Zipp425 Posted August 14, 2006 Author Share Posted August 14, 2006 Nevermind I got it all figured out. Thank you so much for your help. Quote Link to comment 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.