...

View Full Version : Finding in a file, then editing part of the file.



gsnedders
05-26-2004, 10:53 PM
I currently have
$strPagetitle = 'Grand Prix of Australia 2004'; as part of a page, I need a script which will find a line which starts with
$strPagetitle = ' (note the space in the beginning) and will then put what is inside the 's in a TextField, and then you can submit a form and it will change what is inside the 's, how can this be done? It is in a file called australia.php

sad69
05-26-2004, 11:51 PM
I know that it sounds like a PHP question, but I think it's really a PERL question (even though you're using PHP to implement it..). You may want to ask over there cuz they'll know better (I think).. like how to open the file and edit a particular part of a file. I'm assuming you can build the form and pass/accept the needed data from the form..

Sadiq.

gsnedders
05-27-2004, 12:19 AM
I know that it sounds like a PHP question, but I think it's really a PERL question (even though you're using PHP to implement it..). You may want to ask over there cuz they'll know better (I think).. like how to open the file and edit a particular part of a file. I'm assuming you can build the form and pass/accept the needed data from the form..

Sadiq.

OK, but by saying pass/accept I take it you mean check that what's been put in valid?

gsnedders
05-29-2004, 11:22 PM
Found this: http://uk2.php.net/manual/en/function.substr-replace.php
Now I can do it!

How do I do this to another file?

carl_mcdade
05-30-2004, 03:00 AM
First it's hard to visualize but after you realize everytime you do something to a text file you iterates through the file rows and buffers the content then it becomes easy.

I am tired from work now. I will post some code later today after I sleep a bit.

gsnedders
06-01-2004, 02:01 PM
I am tired from work now. I will post some code later today after I sleep a bit.

This is 2 days later, and still no code...

carl_mcdade
06-01-2004, 02:59 PM
Acck sorry!!!!!!

I was tired and forgot I even posted.

Okay let's take a step at a time then I'll test the code. First you need some data.


============= australia.php=================
$strPagetitle = 'Grand Prix of Australia 2004';
$strPageAuthor = 'Alan Henry';
$strPagebook = 'hardcover';
$strPageReview = 'exciting stuff';
$strPageScore = '4 starts';
$strPageAmazonUrl = 'http://www.amazon.com';
============= australia.php=================

============= updateaustralia.php=================
<?php //Load the array with the file rows
$getData = file("ua.php");
$numLines = count( $getData );
$rowKey = "strPageAuthor";
$rowValueString = "Charlie Finlay";


// Look for the right line and set marker
foreach ($getData as $key => $value) {

if(ereg($rowKey,$value)){
$rowMarker = $key;
}
echo "<br>Key: $key; Value:". htmlentities($value)."<br>\n";

}
echo "Update row key is: ".$rowMarker;

// Make changes in the temp variable using the mark
$getData[$rowMarker] = "$".$rowKey. " = '" . $rowValueString."';\n";
er

//Write new file
$putData = fopen ("ua.php", "w");

for($i=0; $i<$numLines; $i++) {

fwrite($putData, $getData[$i]);

}

fclose($putData);
?>
============= updateaustralia.php=================

CODE TESTED NOW!

carl_mcdade
06-01-2004, 06:16 PM
A tip: I would do this in a seperate file and just run through every line regardless. So the form would harvest the entire file and show textfields then a submit sould update every line. Because finding a $ is ***** because it plays havok with all the search function of PHP.

gsnedders
06-01-2004, 06:35 PM
A tip: I would do this in a seperate file and just run through every line regardless.

Why? and it's no problem taking so long, I just needed to be able to work on it today.

carl_mcdade
06-01-2004, 06:59 PM
Well it just become more efficient if you are going to do multiple updates. rather than stopping and doing just that one row update, which could lead to some complicated code later, you just cascade through the lot. You could not do this if the variables are part of some other code. A line break or a code change would mess up your file when you cascaded through the array.

So a seperate file would be a database that could be manipulated without doing harm or giving consideration to other code. I usually do my configuration files like this. I have one update script that can be used on multiple configuartion files.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum