0

I have file(php file) that file inside look like bellow

$php_file_string

//define ( 'name', 'saman' );
   //       define ( 'name', 'saman' );
define ( 'name', 'saman' );
#  define ( 'name', 'saman' );
 /* define ( 'name', 'saman' ); */

i want to replace line un-commented

$search_string :

define ( 'name', 'saman' );

$new_value:

define ( 'name', 'RoX' );

i try using

str_replace($search_string, $new_value, $php_file_string);

this replace all lines(five line inside the file). So above str_replace() is not correctly worked for me. I need something look as bellow result

//define ( 'name', 'saman' );
   //       define ( 'name', 'saman' );
define ( 'name', 'RoX' );
#  define ( 'name', 'saman' );
 /* define ( 'name', 'saman' ); */

please help to me how to replace only un-comented lines in php file

4
  • 1
    use regx to check if line doesn't starts with // or /* and doesn't ends with */ Commented Jul 4, 2013 at 6:15
  • Why don't you remove those lines, if not needed Commented Jul 4, 2013 at 6:31
  • Please refer this stackoverflow.com/questions/834303/… Commented Jul 4, 2013 at 6:34
  • Are you sure you don't want to do what your doing with variables? Commented Jul 4, 2013 at 7:27

2 Answers 2

1

I assumed that, this is the last line (as you mentioned) in your file that you want to change and you have write permission to that, so, symply this should work

$file = 'path/filename.php';
$lines =file($file);
$lines[count($lines)-1]="define ( 'name', 'RoX' );";
if(is_writable($file))
{
    file_put_contents($file, implode($lines));
}

Result would be :

//define ( 'name', 'saman' );
//       define ( 'name', 'saman' );
#  define ( 'name', 'saman' );
/* define ( 'name', 'saman' ); */
define ( 'name', 'RoX' );

Update : (After the question has been edit)

$file = '../mytests/index.php';
$lines = file($file);
for($i=0; $i < count($lines); $i++)
{
    if(substr($lines[$i], 0, 6) === 'define') {
        $lines[$i] = "define ( 'name', 'RoX' );";
    }
}
if(is_writable($file))
{
    file_put_contents($file, implode($lines));
}
Sign up to request clarification or add additional context in comments.

3 Comments

sorry for that, in my example last line(need to replace every un-commented line)
You said only un commented lines and also gave a sample of what you want, so i think the output in my answer matches with your sample, now I'm confused.
sorry for my question i did some mistake. now i edited my question. thankx
0
$u=explode("\n",$php_file_string);//Breaking each line into array
for($i=0;$i<count($u);$i++)
{
if(!substr_count($u[$i],"/*")&&!substr_count($u[$i],"#")&&!substr_count($u[$i],"//"))
//Checking whether line contains any comment or not
$u[$i]=str_replace($search_string, $new_value, $u[$i]);
}
//Now you can use $u as an string array

Try this here we are breaking each line into an array and then checking each line separately for comment if a line is not a comment we replace old value with new one Hope it helps!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.