简体   繁体   中英

Replace only uncomment string with str_replace in php

log.php

define ( 'COMPANY_WEB', 'www.6666.com' );
//define ( 'COMPANY_WEB', 'www.6666.com' );
define ( 'COMPANY_FAX', 'asdfffff' );

My string replace code....

$config_old_arr[0] = " define ( 'COMPANY_WEB', 'www.6666.com' )";
$config_new_arr[0] = " define ( 'COMPANY_WEB', 'www.NEW.com' )";

$config_new_file_content = str_replace($config_old_arr, $config_new_arr, $logfilecontent);

After run my code... change New log.php file

logNew.php

define ( 'COMPANY_WEB', 'www.NEW.com' );
//define ( 'COMPANY_WEB', 'www.NEW.com' );
define ( 'COMPANY_FAX', 'asdfffff' );

but i want to change only value in not-comment line(line No.1) so result string want to be as below

logNew.php

define ( 'COMPANY_WEB', 'www.NEW.com' );
//define ( 'COMPANY_WEB', 'www.6666.com' );
define ( 'COMPANY_FAX', 'asdfffff' );

i like to use str_replace() function but that method may be not suitable for this problem Anyone know solution for this problem please help to me.

use substr to check the first 2 chars of the string

//$linetocheck = line to replace

if (substr($linetocheck, 0, 2) == "//") {

    //the first 2 chars are //

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM