File modification set pointer?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kabuki1985
Forum Commoner
Posts: 32
Joined: Thu Jun 15, 2006 10:19 pm

File modification set pointer?

Post by kabuki1985 »

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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

My first instinct would be to read the file into memory, parse it, change it, then save it again.

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);
The "s" modifier on the end is there to let the (.*?) match the newline - you could definitely refine that more.

Cheers,
Kieran
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

I tried to do in different way but ended on different.

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));
	} 
	
}
?>
Output became :

Code: Select all

id=>300
name=>Michael
id=>400
name=>Bob
id=>500
name=>Sam
id=>600
501e=>JaneSammy
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

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

Post by kabuki1985 »

I'm not good at regular expression.
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=>17

To

Code: Select all

....
id=>501
name=>Sammy
age=>18 
.....
kabuki1985
Forum Commoner
Posts: 32
Joined: Thu Jun 15, 2006 10:19 pm

Post by kabuki1985 »

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);
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

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);
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

omg! I didn't know you could do that:

Code: Select all

<?php /* yadda.php */
return array ( ...

Code: Select all

$users = require 'yadda.php';
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

ok.. here they are as functions:

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);
}
Cheers,
Kieran
Post Reply