replace a specific line in a php file

Status
Not open for further replies.

techdaemon

Banned
Banned
663
2010
5
0
Hi. I'm just a noob in php. I need to know how I can replace a specific line of text in a php file. For example, in a php file below named conf.php, I want 'used_dir' => 'files/',, which is in the third line, to be replaced with 'used_dir' => 'new_files/',. How can i do this? Any reply will be appreciated.

PHP:
<?php
$options = array (
'used_dir' => 'files/',
'login' => true,
);
?>

PS: I was thinking I can apply file_get_contents() and file_put_contents() functions in this but I don't have an idea how to.
 
10 comments
PHP:
<?php

//Get content of your php file.
$buffer = file_get_contents('yourphpfile.php');

//Replace using RegEx
$buffer = preg_replace("/'used_dir' => 'files\/'/", "'used_dir'=> 'new_files'", $buffer);

//output.
echo $buffer;

?>
 
what if line no. 3 is not always 'used_dir' => 'files/', but a variable that changes? what im trying to do exactly is creating a form using another php file to edit conf.php. so i was wondering if i can do this method

(1) chmod conf.php to 777, read and copy the file with file_get_contents()
(2) create a new file called conf_new.php and write in it the copied content using file_put_contents()
(3) replace line no. 3 with something else
(4) delete conf.php with unlink(),
(5) rename conf_new.php to conf.php
 
dude, you donot need to edit the file manually, when adding a variable in the regEx, it will automatically be updated.
 
change the config like that and define $new when you want to use dir new_files ?

PHP:
<?php 
if ($new==1) {
$options = array ( 
'used_dir' => 'new_files/', 
'login' => true, 
); 
}

else
{
$options = array ( 
'used_dir' => 'files/', 
'login' => true, 
);
} 
?>
 
@NucleA and Gav0: When posting PHP code please use the
PHP:
[/B] tag, it makes it easier to read. I've edited your posts and put the [B][PHP][/B] tag in them.
 
PHP:
<?php


$options = array ( 
'used_dir' => 'files/', 
'login' => true, 
); 
$newname = "newname"; // Here enter new name u wants 
echo preg_replace("/[a-zA-Z0-9]+/",$newname,$options['used_dir']);

?>
 
Am I the only one that has noticed the real problem? Why would you hard-code settings in the first place. Editing PHP files is not how you should change settings. Either use a database or plain text (XML?) to store and alter settings. You don't need to travel trough China in order to cross the street. Programming is all about logic. Think logically ;).
 
Use var_export

PHP:
<?php

//Load your file and change values like so:
include 'yourphpfile.php';
$options['user_dir'] = 'New String';

//var_export prints the result in a way it can be read again by PHP
$contents = "<?php\r\n" . ' $options = ' . var_export($options,true) . "\r\n" . ' ?>';

//Put the new contents in the file.
file_put_contents('yourphpfile.php',$contents);
?>

Your new contents would look like so:

PHP:
<?php
$options = array(
    'used_dir' => 'New String', 
    'login' => true, 
)
?>

No too hard!

http://php.net/manual/en/function.var-export.php
 
Status
Not open for further replies.
Back
Top