Replace a line of code in a php file using php
To replace the below line of code in the testreplace.php
define(‘PATH_CURRENT_SITE’, ‘/mydoubts/’);
this should be changing to
define(‘PATH_CURRENT_SITE’, ‘/vasanthan/’);
For this please execute the stringreplace.php… changes will reflect in the testreplace.php
#File-stringreplace.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?php $newstring="vasanthan"; $mystring="define('PATH_CURRENT_SITE', '/mydoubts/');"; $toFind = "/"; $start = 0; $firstpos = strpos($mystring,$toFind,$start); $start_pos=$firstpos+1; $start=$firstpos+1; $secondpos=strpos($mystring,$toFind,$start); $end_pos=$secondpos-1; $length=$end_pos-$start_pos+1; $convertedstring=substr_replace($mystring,$newstring,$start_pos,$length); $Filename_to_change = 'testreplace.php'; $new_string = $convertedstring; $string_to_replace = $mystring; if(file_exists($Filename_to_change)) { $File_content = file_get_contents($Filename_to_change); // Replace the text $Value_after_replace = str_replace($string_to_replace, $new_string, $File_content); // Open the file $File_open_for_write = fopen($Filename_to_change, 'w+'); // Write the text into it $File_write = fwrite($File_open_for_write, $Value_after_replace); if($File_write) { // If success, close file and return true fclose($File_open_for_write); return true; } // If failure, close file and return false fclose($File_open_for_write); return false; } else { die('File' . $Filename_to_change . 'does not exist!'); } ?> |
2.#File-testreplace.php
1 2 3 4 5 6 7 8 |
<?php define('MULTISITE', true); define('SUBDOMAIN_INSTALL', false); define('DOMAIN_CURRENT_SITE', 'mydoubts.in'); define('PATH_CURRENT_SITE', '/mydoubts/'); define('SITE_ID_CURRENT_SITE', 1); define('BLOG_ID_CURRENT_SITE', 1); ?> |