File modification set pointer?
Moderator: General Moderators
-
kabuki1985
- Forum Commoner
- Posts: 32
- Joined: Thu Jun 15, 2006 10:19 pm
File modification set pointer?
Hi, I have this text
id=>300
name=>Michael
id=>400
name=>Bob
id=>500
name=>Sam
id=>600
name=>Jane
How do i open the file and point to a line for example at the start of id=>500 so that i can overwrite it with
id=>501
name=>Sammy
while remaning the following below
id=>600
name=>Jane
id=>300
name=>Michael
id=>400
name=>Bob
id=>500
name=>Sam
id=>600
name=>Jane
How do i open the file and point to a line for example at the start of id=>500 so that i can overwrite it with
id=>501
name=>Sammy
while remaning the following below
id=>600
name=>Jane
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
My first instinct would be to read the file into memory, parse it, change it, then save it again.
Something like:
The "s" modifier on the end is there to let the (.*?) match the newline - you could definitely refine that more.
Cheers,
Kieran
Something like:
Code: Select all
$file_contents=file_get_contents('./people.to.kill.txt');
$old_id='500';
$new_id='501';
$new_name='Sammy';
$file_contents = preg_replace("/id=>".$old_id."(.*?)name=>(\w*)/s","id=>".$new_id."\nname=>".$new_name,$file_contents);
file_put_contents('./people.to.kill.txt',$file_contents);Cheers,
Kieran
- dibyendrah
- Forum Contributor
- Posts: 491
- Joined: Wed Oct 19, 2005 5:14 am
- Location: Nepal
- Contact:
I tried to do in different way but ended on different.
Output became :
Here, the strings are appended and also replaced.
Can someone rectify what is to be done in this code to give the following output ?
Code: Select all
<?php
$f = "users.txt";
$hn = fopen($f, "r+");
$find_name = 'Jane';
$replace_name = 'Sammy';
$find_key = '600';
$replace_key = '501';
while(!feof($hn)){
$line = fgets($hn);
$tmp_array = explode("=>", $line);
if(trim($tmp_array[0])=='name' && trim($tmp_array[1])==$find_name){
fwrite($hn, $replace_name, strlen($replace_name));
}
if(trim($tmp_array[0])=='id' && trim($tmp_array[1])==$find_key){
fwrite($hn, $replace_key, strlen($replace_key));
}
}
?>Code: Select all
id=>300
name=>Michael
id=>400
name=>Bob
id=>500
name=>Sam
id=>600
501e=>JaneSammyCan someone rectify what is to be done in this code to give the following output ?
Code: Select all
id=>300
name=>Michael
id=>400
name=>Bob
id=>500
name=>Sam
id=>501
name=> Sammy-
kabuki1985
- Forum Commoner
- Posts: 32
- Joined: Thu Jun 15, 2006 10:19 pm
I'm not good at regular expression.
What if i want to add one more attribute?
To
What if i want to add one more attribute?
Code: Select all
id=>300
name=>Michael
age=>12
id=>400
name=>Bob
age=>15
id=>500
name=>Sam
age=>13
id=>600
name=>Jane
age=>17To
Code: Select all
....
id=>501
name=>Sammy
age=>18
.....-
kabuki1985
- Forum Commoner
- Posts: 32
- Joined: Thu Jun 15, 2006 10:19 pm
Don't really understand regex too well. But playing around, i got this.
Code: Select all
$file_contents = preg_replace("/id=>".$old_id."(.*?)name=>(.*?)age=>(\w*)/s","id=>".$new_id."\nname=>".$new_name."\nage=>".$new_age,$file_contents);You could also use var_export and include to stare the values, no need to write a parser on yourself.
Code: Select all
<?php /* yadda.php */
return array (
300 =>
array (
'name' => 'Michael',
'age' => 12,
),
400 =>
array (
'name' => 'Bob',
'age' => 15,
),
500 =>
array (
'name' => 'Sam',
'age' => 13,
),
600 =>
array (
'name' => 'Jane',
'age' => 17,
),
);Code: Select all
<?php
$users = require 'yadda.php';
$users[300]['age'] += 1;
$newContents = "<?php /* yadda.php */\r\nreturn " . var_export($users, true) . ";";
file_put_contents('yadda.php', $newContents);- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
omg! I didn't know you could do that:
Code: Select all
<?php /* yadda.php */
return array ( ...Code: Select all
$users = require 'yadda.php';- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
ok.. here they are as functions:
Cheers,
Kieran
Code: Select all
$old_id='500';
$new_id='501';
$new_name='Sammy';
$filename='./people.to.kill.txt';
function change_entry($filename,$old_id,$new_id,$new_name){
$file_contents = file_get_contents($filename);
$file_contents = preg_replace("/id=>".$old_id.".*?name=>\w*/s","id=>".$new_id."\nname=>".$new_name,$file_contents);
file_put_contents($filename,$file_contents);
}
function add_entry_after($filename,$after_id,$new_id,$new_name){
$file_contents = file_get_contents($filename);
$file_contents = preg_replace("/id=>".$after_id.".*?name=>\w*/s","$0\nid=>".$new_id."\nname=>".$new_name,$file_contents);
file_put_contents($filename,$file_contents);
}
function add_entry_before($filename,$before_id,$new_id,$new_name){
$file_contents = file_get_contents($filename);
$file_contents = preg_replace("/id=>".$before_id.".*?name=>\w*/s","id=>".$new_id."\nname=>".$new_name."\n$0",$file_contents);
file_put_contents($filename,$file_contents);
}Kieran