Stats: 3,190,376 members, 7,940,444 topics. Date: Thursday, 05 September 2024 at 06:00 AM |
Nairaland Forum / Science/Technology / Programming / Need Help On This Basic Php (3693 Views)
For Young And Basic Web Development HOP IN / Pls After Learning D Basic Php Wat Next? / Keywords / Reserved word In Q-BASIC (2) (3) (4)
Need Help On This Basic Php by paranorman(m): 10:17am On Feb 20, 2016 |
Let's say I have a 'names.txt' file containing: john;jack james;jill alex;wellington each line contains surname and first-name delimited by a semcolon, each line is escaped by \n. Say I want to modify 'alex', how can I do this without overwriting the whole file itself? I want a code memory friendly, so don't use file() function. Don't overwrite the whole text file, just modify your string of interest. Cc: dhtml18 |
Re: Need Help On This Basic Php by paranorman(m): 10:18am On Feb 20, 2016 |
Oya na |
Re: Need Help On This Basic Php by Nobody: 11:03am On Feb 20, 2016 |
<?phpAnalyze this. . . |
Re: Need Help On This Basic Php by paranorman(m): 11:15am On Feb 20, 2016 |
dhtml18:i know this one too bro. Done it. But's still uses an extended file() function. I am talking....: fopen while (!feof..) fgets ..i used these, but the code is unnessarily long and uses lots of manipulations and overwrites the whole file. You can do better. |
Re: Need Help On This Basic Php by asalimpo(m): 1:04pm On Feb 20, 2016 |
sorry bro but you have to overwrite the whole file to save your changes. |
Re: Need Help On This Basic Php by paranorman(m): 1:23pm On Feb 20, 2016 |
asalimpo: nope bro, you don't have to. Do a google search on that and you'd see some cool things. Thing is, I am not satisfied. |
Re: Need Help On This Basic Php by asalimpo(m): 3:00pm On Feb 20, 2016 |
You read the file into memory, change its content save changes to disk - how dyu do this without overwriting the file? It's impossible. I don't need to google squat. Maybe, all your interested in,is maintaining the time stamp on the file. So that after overwriting content, the stamp remains unchanged. |
Re: Need Help On This Basic Php by asalimpo(m): 3:00pm On Feb 20, 2016 |
You read the file into memory, change its content save changes to disk - how dyu do this without overwriting the file? It's impossible. I don't need to google squat. Maybe, all you're interested in,is maintaining the time stamp on the file. So that after overwriting content, the stamp remains unchanged. |
Re: Need Help On This Basic Php by paranorman(m): 3:16pm On Feb 20, 2016 |
asalimpo:okay then.. Can I get your version of the code? Don't use the file() or any extention of it to load the file itself into memory. |
Re: Need Help On This Basic Php by ChinenyeN(m): 4:03pm On Feb 20, 2016 |
There's no possible way to change a files contents without overwriting the file. Each time a file is accessed, it's contents are read into memory and held in some buffer. Once the user is done manipulating it's contents it gets written back to disk. That is the essence of file IO functions. In fact, thats exactly how text editors work. There's no way around this. One can't be selective and only manipulate a portion of the file while it is still on the disk. The only thing that exists as remotely close to what your asking is to append content to the end of the file. Doing that will not truncate pre-existing content, but it will still require some IO functions. |
Re: Need Help On This Basic Php by asalimpo(m): 5:00pm On Feb 20, 2016 |
paranorman:I dont do php. |
Re: Need Help On This Basic Php by Nobody: 7:58pm On Feb 20, 2016 |
Use a database bro, this your line of programming reasoning is not normal. If I know the aim of your task, and the objective, maybe I can help you. But as far as i am concerned, you are talking in riddles. paranorman:Even if you use fopen, and you manipulate the file, you still need to save it. What you are asking for to me does not really make sense - whether in PHP or any other language. We need to understand precisely what you are trying to achieve, maybe we can help you. . . . THERE IS NO WAY IN ANY PROGRAMMING LANGUAGE, that you can change the content of an existing file without overwriting it to save changes. Even databases sef at some level use files to save the contents of the database - it does not use magic. 1 Like |
Re: Need Help On This Basic Php by paranorman(m): 8:39pm On Feb 20, 2016 |
dhtml18: sorry I got you guys mistaken.. What I am trying to archieve here is writing a code that changes your string of interest only.( a fraction of your strings without having to work on the whole strings and then modify your string of interest? You dig? for example dhtml18, the code you wrote at first only worked on that particular string. You get what I am saying? You modified the particular string("alex" ![]() I think I abused the 'overwrite' word, so it got you all confused. So, instead of using the file() function or any extension of it, use fopen and its companions and work on "alex" only.. You talking dbms, i know. I could use that. But this is just one little challenge. Ain't nothing serious. Just trying to avoid the file() functions and its extensions |
Re: Need Help On This Basic Php by Nobody: 9:08pm On Feb 20, 2016 |
For the first time ever, I had to concede defeat on a PHP matter. Reason: Mission Impossible! 1 Like |
Re: Need Help On This Basic Php by Adesege(m): 9:58pm On Feb 20, 2016 |
Hello, paranorman, firstly, may I know what method you want to use to edit the text file? Is it through an html form? If yes, try the below code. Please note that I haven't tested it but it should work. <? if($_POST['Submit']){ $open = fopen("textfile.txt","w+" ![]() $text = $_POST['update']; fwrite($open, $text); fclose($open); echo "File updated.<br />"; echo "File:<br />"; $file = file("textfile.txt" ![]() foreach($file as $text) { echo $text."<br />"; } }else{ $file = file("textfile.txt" ![]() echo "<form action=\"".$PHP_SELF."\" method=\"post\">"; echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">"; foreach($file as $text) { echo $text; } echo "</textarea>"; echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n </form>"; } ?> Also, like it has been suggested, making use of a DBMS will be great. There is no way you will not be using the file() function or it's extensions to read a file in PHP. Please let me know if this helps you. Regards |
Re: Need Help On This Basic Php by ChinenyeN(m): 10:26pm On Feb 20, 2016 |
For the record, a call to file_get_contents is equivalent to calling fopen, fread and fclose, and according to the PHP manual, it is similar to the file function, except that file_get_contents returns the contents as a string. So you can assign it to a variable and do any sort of string manipulation with it. file_get_contents is not an extension of file, both have different return values (file returns an array). If anything, both file_get_contents and file are wrappers for or extensions of the main file IO functions in PHP (i.e. fopen, fread, fwrite, fclose). Anyway, if all you're looking to do string manipulation, then file_get_contents is the functionality you want to use. It will give you the file contents as a string and you can do what you want. Once you're done, call file_put_contents (which is equivalent to calling fopen, fwrite and fclose), if you need to save the changes, just as in the example provided by dhtml18. |
Re: Need Help On This Basic Php by Nobody: 12:51am On Feb 21, 2016 |
paranorman:The code i supplied is actually better than this and can be shorten as: <?php It can still be further shortened as: <?php I am still waiting to see someone else do better, maybe I missed out something in the php manual. . . . |
Re: Need Help On This Basic Php by paranorman(m): 7:51am On Feb 21, 2016 |
ChinenyeN: hey, boss I tink I am talking too program contextual than program literal. Okay, file pointer should return an arrray, not string. Do not use functions that has got the word 'file' innit ( like file(), readfile(), file_get_contents() e.t.c...) you feel me? So understand me bros, dhtml18, |
Re: Need Help On This Basic Php by Nobody: 8:00am On Feb 21, 2016 |
Na you go solve your problem yourself, when you have solve it, bring the solution here. . . . . |
Re: Need Help On This Basic Php by paranorman(m): 11:21am On Feb 21, 2016 |
dhtml18: hehehehehe.. When I get on PC, I will post here. Bros, you self think am na |
Re: Need Help On This Basic Php by Nobody: 12:11pm On Feb 21, 2016 |
^^^I am not thinking anything on this one, you need to provide a justifiable reason why you want to do something that PHP has provided functions for. Maybe you are solving a quiz somewhere, or have got the whole thing wrong. Your approach of the matter is unconventional and a sheer waste of time, i have provided the only code i am going to. . . .am waiting for you to solve it yourself. |
Re: Need Help On This Basic Php by paranorman(m): 1:36pm On Feb 21, 2016 |
dhtml18: okay bro, when using PC, post it I shall. |
Re: Need Help On This Basic Php by Nobody: 1:43pm On Feb 21, 2016 |
What i am suspecting is that, you do not seem to have the right words to describe what you have in mind. Possibly, you might be talking of file pointers and all that. . .but unless you find the right words to describe what you seek, even google wont be able to help. But whatever the case is, doing it without file, fwrite, fopen,fwrite, file_get_contents, file_put_contents and their families, am so waiting to see that. |
Re: Need Help On This Basic Php by Nmeri17: 4:34pm On Feb 21, 2016 |
All of you up there are big Olodos ![]() ![]()
|
Re: Need Help On This Basic Php by Nobody: 4:50pm On Feb 21, 2016 |
See as my mouth open, no fit close again! |
Re: Need Help On This Basic Php by Djade007: 5:34pm On Feb 21, 2016 |
Make we see the content of that class 1 Like |
Re: Need Help On This Basic Php by Nmeri17: 6:48pm On Feb 21, 2016 |
Djade007:egbon if you want the contents, go to OP's house and collect it ![]() |
Re: Need Help On This Basic Php by Djade007: 6:58pm On Feb 21, 2016 |
Nmeri17: Sorry jare, I think say na you be the op ni. @op na the code wey you dey search for be that, I don run am and it worked seamlessly |
Re: Need Help On This Basic Php by Nobody: 7:10pm On Feb 21, 2016 |
Nmeri17:Yes o, and i have been giving this thread close marking to find that solution. . . .i am thinking it is about time i quit programming (since I be olodo). |
Re: Need Help On This Basic Php by Djade007: 7:16pm On Feb 21, 2016 |
Ooo oga dhtml18... You should start researching o because op specifically mentioned you. |
Re: Need Help On This Basic Php by Nmeri17: 7:31pm On Feb 21, 2016 |
Djade007:Ehen! ![]() ![]() ![]() ![]() |
Re: Need Help On This Basic Php by ChinenyeN(m): 7:40pm On Feb 21, 2016 |
paranorman: Truthfully, you cannot get a file's contents unless you open the file. Additionally, you cannot open a file without first loading it into memory. This part has little to nothing to do with programming, and everything to do with how computers are designed. Whenever you tell the computer that you want to use a certain file on your hard disk, the computer takes your command, locates the file and makes it available for you in RAM. Even the operating system that manages your file request is running in RAM. It's simply how computers work. And on the programming side, it is the functions like fopen, file and file_get_contents that allow us to manage our file requests. In simple terms, that is how the implementation works. Even if you used another programming language and they had a different function called whatever, it would still make the same request to the OS as fopen, file and etc. Now, if your only objective is to get the file contents as an array, without using any function with "file" in it, then I think I understand. That is something different altogether. It can easily be done using fopen, fgetcsv and fclose (the word "file" is not written out in these functions).
|
Please Help Look At My Code (what is wrong with my code ) / Where Can I Buy Programming Books In Lagos / Learn Programming For Free And Earn Stipends
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 64 |