Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,207,663 members, 7,999,889 topics. Date: Monday, 11 November 2024 at 03:42 PM

Need Help On This Basic Php - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Need Help On This Basic Php (3729 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)

(1) (2) (Reply) (Go Down)

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
<?php
//read names file to string
$names=file_get_contents('names.txt');

//replace the name alex with dhtml18
$names=str_replace('alex','dhtml18',$names);

//display result on screen
echo nl2br($names);

//save the updated file
//file_put_contents('names.txt',$names);
?>
Analyze this. . .
Re: Need Help On This Basic Php by paranorman(m): 11:15am On Feb 20, 2016
dhtml18:

Analyze this. . .
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:
sorry bro but you have to overwrite the whole file to save your changes.

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

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

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


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.

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"wink only.

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+"wink;
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />";
echo "File:<br />";
$file = file("textfile.txt"wink;
foreach($file as $text) {
echo $text."<br />";
}
}else{
$file = file("textfile.txt"wink;
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:

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.
The code i supplied is actually better than this and can be shorten as:

<?php
$names=file_get_contents('names.txt'); //read names file to string
$names=str_replace('alex','dhtml18',$names); //replace the name alex with dhtml18
file_put_contents('names.txt',$names); //save the updated file
?>

It can still be further shortened as:

<?php
$names=str_replace('alex','dhtml18',file_get_contents('names.txt')); //read file and replace the name alex with dhtml18
file_put_contents('names.txt',$names); //save the updated file
?>


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

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:
Na you go solve your problem yourself, when you have solve it, bring the solution here. . . . .

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:
^^^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.

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 angry in fact, you all should collectively get a mention whenever "Olodo" is typed on nairaland undecided that said, we get straight to business. OP, here is the code variant you have sought all your life!


<?php
include " paranorman_miracle_working_class.inc";

$cached_file = PMWC::para_read(PMWC::para_open ("names.txt", PMWC::size("names.txt"wink, "r+"wink;

$swap_string = PMWC::replace ("nmeri17 is a god", "Alex", $cached_file);

$case_closed = PMWC::para_read($swap_string, "x"wink;

if ($case_closed) {
echo "paranorman is a nice monicker" . "\wink ";
}
?>
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:
Make we see the content of that class
egbon if you want the contents, go to OP's house and collect it undecided is he not the one that said he has a miracle working module besides file () that can handle it perfectly??
Re: Need Help On This Basic Php by Djade007: 6:58pm On Feb 21, 2016
Nmeri17:
egbon if you want the contents, go to OP's house and collect it undecided is he not the one that said he has a miracle working module besides file () that can handle it perfectly??

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:
egbon if you want the contents, go to OP's house and collect it undecided is he not the one that said he has a miracle working module besides file () that can handle it perfectly??
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:

@op na the code wey you dey search for be that, I don run am and it worked seamlessly
Ehen! grin grin grin grin thank you
Re: Need Help On This Basic Php by ChinenyeN(m): 7:40pm On Feb 21, 2016
paranorman:
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...)

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

<?php

$names = []; // declare here for scope purposes

// make sure file can be accessed
if(($file = fopen( "names.txt", "r" )) !== FALSE) {

// read each line in the file
// you have the names delimited by semilcolon, so use that in the function
while(($line = fgetcsv($file, ";" )) !== FALSE) {

// put the contents into $names array
$names[] = array($line[0], $line[1]);

}

// done with the file, so we can now close it
fclose($file);

}

// print the names in the $names array
print_r($names);

// Should Give you
/***
* Array (
* [0] => Array (
* [0] => john
* [1] => jack
* )
* [1] => Array (
* [0] => james
* [1] => jill
* )
* [2] => Array (
* [0] => alex
* [1] => wellington
* )
* )
***/
?>

(1) (2) (Reply)

UPDATED: Third Person 3D Game Developed In Unity3d / Ways To Easily Get Content For Your Blog Or Website / Weekend Challenge For Practice: RLE Encoding

Viewing this topic: 1 guest(s)

(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. 59
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.