PHP Modify a single line in a text file

后端 未结 5 1993
感情败类
感情败类 2021-01-05 16:00

I tried and looked for a solution, but cannot find any definitive.

Basically, I have a txt file that lists usernames and passwords. I want to be able to change the p

5条回答
  •  无人及你
    2021-01-05 16:35

    I think when you get that file use file_get_contents after that use preg_replace for the particular user name

    I have done this in the past some thing like here

                   $str = "";
    
           $reorder_file = FILE_PATH;
           $filecheck = isFileExists($reorder_file);
    
           if($filecheck != "")
            {
                $reorder_file = $filecheck;
            }
            else
            {
                errorLog("$reorder_file :".FILE_NOT_FOUND);
                $error = true;
                $reorder_file = "";
            }
            if($reorder_file!= "")
            {
                $wishlistbuttonhtml="YOUR PASSWORD WHICH YOU WANT TO REPLACE"
                $somecontent = $wishlistbuttonhtml;
                $Handle = fopen($reorder_file, 'c+');
                $bodytag = file_get_contents($reorder_file);
                $str=$bodytag;
                $pattern = '/(YOUR_REGEX_WILL_GO_HERE_FOR_REPLACING_PWD)/i';
                $replacement = $somecontent;
                $content = preg_replace($pattern, $replacement, $str,-1, $count);
                fwrite($Handle, $content); 
                fclose($Handle);
            }   
    

    Hope this helps....

提交回复
热议问题