Results 1 to 10 of 11
-
25th Sep 2010, 12:59 PM #1OPBanned
replace a specific line in a php file
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 Code:<?php
$options = array (
'used_dir' => 'files/',
'login' => true,
);
?>techdaemon Reviewed by techdaemon on . replace a specific line in a php file 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 $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 Rating: 5
-
25th Sep 2010, 01:04 PM #2MemberWebsite's:
Pirateview.org Gfxs.org StarkWood.orgPHP Code:<?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;
?>
-
25th Sep 2010, 01:22 PM #3OPBanned
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
-
25th Sep 2010, 01:54 PM #4MemberWebsite's:
Pirateview.org Gfxs.org StarkWood.orgit simple, just add the variable name in the regular expression!
-
25th Sep 2010, 02:07 PM #5OPBanned
i dont want to edit that file in filemanager or ftp!
-
25th Sep 2010, 03:51 PM #6MemberWebsite's:
Pirateview.org Gfxs.org StarkWood.orgdude, you donot need to edit the file manually, when adding a variable in the regEx, it will automatically be updated.
-
25th Sep 2010, 03:59 PM #7Member
change the config like that and define $new when you want to use dir new_files ?
PHP Code:<?php
if ($new==1) {
$options = array (
'used_dir' => 'new_files/',
'login' => true,
);
}
else
{
$options = array (
'used_dir' => 'files/',
'login' => true,
);
}
?>
-
25th Sep 2010, 04:53 PM #8Respected Member
-
25th Sep 2010, 05:49 PM #9Respected DeveloperPHP Code:
<?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']);
?>Bots Development | Web Development | Wordpress Customization | PSD Conversion
Life has many twists and turns in it, you have to take what you are given and use it for the best.
-
25th Sep 2010, 07:34 PM #10Respected Developer
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
.
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Delete specific file in each folder
By Ruriko in forum Technical Help Desk SupportReplies: 6Last Post: 6th Oct 2012, 01:05 AM -
[Blogger] Code to live replace specific words
By nICEsHARE in forum Web Application/Script SupportReplies: 0Last Post: 2nd Aug 2011, 01:01 PM -
Debian - command line. Replace () in filenames with _
By AndroidApps in forum Web Development AreaReplies: 2Last Post: 9th Jul 2010, 09:30 AM -
Specific File Hosts Sites
By DeLeTeD in forum Forum and DDL DiscussionReplies: 13Last Post: 5th Apr 2010, 10:10 PM
themaLeecher - leech and manage...
Version 5.03 released. Open older version (or...