Please help ...

It is necessary to change the file line by line, the content of which is approximately the following:

<?php if(!defined('CMS')) { die('Отказано в доступе!');} $CFG = array(); $_CFG['SITENAME']='бла бла'; $_CFG['HOMETITLE']='бла бла'; $_CFG['HOMECOM']=''; $_CFG['SITEOFF']=0; $_CFG['DEBUG']=0; $_CFG['OFFTEXT']='Производится обновление сайта'; $_CFG['KEYWORDS']='бла бла'; $_CFG['METADESC']='бла бла'; $_CFG['SEOURL']=''; $_CFG['LANG']='ru'; $_CFG['SITEMAIL']='admin@sait.ru'; $_CFG['TEMPLATE']='tpl'; $_CFG['TOMEZONA']='Asia/Irkutsk'; $_CFG['TIMEDIFF']=''; $_CFG['ALLOW_IP']=''; ?> 

I tried the following code, but it crumbles the whole file to horror

  $f_arr = file($dir. "/includes/config.php"); $f_arr[2] ='$_CFG[\'SITENAME\']=\''.$sitename.'\';'; print_r( $f_arr); $f = fopen($dir. "/includes/config.php" , "w" ); for( $i = 0; $i < count( $f_arr ); $i++ ) { fwrite( $f , $f_arr[$i]."\n"); } 

Adding "left" spaces, tried $ f_arr [2]. = "\ N" and from

fwrite ($ f, $ f_arr [$ i]. "\ n");

remove

"\ n"

but there is something better, but you still need to have the file as originally, and only one line has changed and nothing else can be rearranged or raped. If there is a good man, I will be very grateful to him, it's just like a trifle, but my head hurts on him.

  • Line by line? Sed - neoascetic
  • I tried the following code, I will be very grateful to him, And by the nickname you will not say that this is a guy .... - Alexey Lobanov
  • The 30th rule of the internet is neoascetic

2 answers 2

Use Zend_Code_Generator .

Although, in relation to this situation, I would advise storing the config in ini . Tools for working with ini complete, the same Zend_Config .

    In this version it works:

     <?php $sitename='11111111'; $f_arr = file("./config.php"); $f_arr[2] = '$_CFG[\'SITENAME\']=\''.$sitename.'\';'."\n";//тут двойные кавычки после одинарных для нормального переноса print_r($f_arr); $f = fopen("./config.php", "w"); for ($i = 0; $i <= count($f_arr); $i++) { fwrite($f, $f_arr[$i]);//тут перенос не нужен(он в каждой строке уже есть) } ?> 

    Although, in my opinion, it is still ugliness.

    • 2
      In such anguish, a certain PHOTO_CMS was born))))) It’s even scary to think how other components can work there, if such a binge is with updating the config. - Sharpness
    • It just happens that I forgot about working with files. CMS for myself writing. By the way, swears on on> fwrite ($ f, $ f_arr [$ i]); - Sexy Girl