I want to ovewrite some labeled data in PHP Config file which looks like this:
define("__dbhost__", '{DB_HOST}');
define("__dbname__", '{DB_NAME}');
define("__dbuser__", '{DB_USER}');
define("__dbpass__", '{DB_PASS}');
define("__dbport__", '{DB_PORT}');
So the file is really simple.
The way i'm trying to replace data is by:
$searchF = array('{DB_HOST}','{DB_NAME}','{DB_USER}','{DB_PASS}', '{DB_PORT}');
$replaceW = array('a','c','d','b','a');
$fname = "../app/config/database.php";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));
$content = str_replace($searchF, $replaceW, $content);
$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);
But once finished and after I open the file I get
Resource id #16
Inside of it.
What's going on and why typical str_replace won't work for this case?
Just read the entire file into a string:
$searchF = array('{DB_HOST}','{DB_NAME}','{DB_USER}','{DB_PASS}', '{DB_PORT}');
$replaceW = array('a','c','d','b','a');
$fname = "../app/config/database.php";
$content = file_get_contents($fname);
$content = str_replace($searchF, $replaceW, $content);
file_put_contents($fname, $content);
Read file into string
Replace on that string
Write string to file
Related
I have a txt file generated by an instrument. The file needs some sanitation before the data is read into a mysql table. I have done this. But now due to some problem the data generated by the instruments contains empty lines in between and I want to remove it.
At present this is the code
$target=$_FILES["report_file"]["tmp_name"];
$file_contents = file_get_contents($target);
$file_contents = str_replace("?","",$file_contents);
$file_contents = str_replace(" ","",$file_contents);
$file_contents = str_replace("","",$file_contents);
$file_contents = str_replace("~","",$file_contents);
$file_contents = str_replace("","",$file_contents);
$file_contents = str_replace("#","",$file_contents);
$file_contents = str_replace(","," ",$file_contents);
file_put_contents($target,$file_contents);
$f = fopen($target, "r");
Can I use file_skip_empty_lines with file_put_contents? of how to put contents without empty lines?
You can trim empty lines using preg_replace
Trim whitespaces at the beginning of line
$file_contents = preg_replace("!^s+!m", "", $file_contents);
Remove empty lines
$file_contents = preg_replace("![\n\r]+\s*[\n\r]+!", "\r\n", $file_contents);
I'm trying to modify my txt file what I'm using in dokuwiki.
I generate timestamp on top of the txt file like this:
function filecont($file,$data)
{
$fileContents = file($file);
array_shift($fileContents);
array_unshift($fileContents, $data);
$newContent = implode("\n", $fileContents);
$fp = fopen($file, "w+");
fputs($fp, $newContent);
fclose($fp);
}
And my original txt file looks like this:
Now when I use my function:
$txt= "Last generated: " . date("Y M D h:i:s");
filecont($file,$txt);
I get a result like this:
Now I don't want to remove my ====== Open IoT book ======, it's probably because I don't have empty space in the first line?
But the worst problem that I have Is that is generates many empty spaces what I don't want.
I only want to get last generated at the top of the txt file and anything else untouched
I tested your code and removed the extra newlines by changing the line:
$fileContents = file($file);
to
$fileContents = file($file, FILE_IGNORE_NEW_LINES);
Adding the FILE_IGNORE_NEW_LINES flag stops a newline being added to each element/line.
http://php.net/manual/en/function.file.php.
I also removed the array_unshift(), which leaves '====== Open IoT book ======' in the file.
So my final function looked like this:
function filecont($file,$data)
{
$fileContents = file($file, FILE_IGNORE_NEW_LINES);
//array_shift($fileContents); Removed to preserve '====== Open IoT book ======' line.
array_unshift($fileContents, $data);
$newContent = implode("\n", $fileContents);
$fp = fopen($file, "w+");
fclose($fp);
}
Might just delete this line
array_shift($fileContents);
solve your problem?
when you get elements of file you need to check whether Last generated: is as your first row or not accordong to it yu need to use array_shift
$fileContents = file($file);
if(stripos($fileContents[0],"Last generated:") !== false)
{
array_shift($fileContents); //if found use shift
}
array_unshift($fileContents, $data);
$a = trim($allDataInSheet [$i]["A"]);
$b=trim($allDataInSheet [$i]["B"]);
/* copy the source file */
$fname = "edm.html";
copy("edm.html","hk/edm.html");
$fcopy="hk/edm.html";
/* read the duplicate file */
$fhandle = fopen($fcopy,"r");
$content = fread($fhandle,filesize($fcopy));
/* replace the string in the duplicate file */
$content = str_replace($a,$b, $content);
$fhandle = fopen($fcopy,"w");
fwrite($fhandle,$content);
fclose($fhandle);
Above code was not working properly,I dont want to replace the string inside the source file.I need to take the duplicate of the source file and change the strings.Thanks in advance.Pleaes help me to fix it..
I think this may be simpler if you just use file_get_contents and file_put_contents.
$fname = 'edm.html';
copy('edm.html', 'hk/edm.html');
$fcopy = 'hk/edm.html';
// read the file
$content = file_get_contents($fcopy);
// make the replacement
$content = str_replace($a, $b, $content);
// write the file
file_put_contents($fcopy, $content);
Also if you put
error_reporting(E_ALL);
ini_set('display_errors', 1);
at the start of your script you will make sure that you are seeing all PHP errors.
I'm loading a file by this script:
$hFile = fopen($sFile, "r");
$sContent = "";
while(!feof($hFile)) {
$sContent .= fread($hFile, 4096);
}
fclose($hFile);
It works as it should do, but i tried to load a file called test.txt
which contains the following string: <>863b?)(/&(§&/))!)!=WLKM! K!*ÜQWW!W3³³w2_:LPE
The variable $sContent now doesn't contain anything.
$html = mb_convert_encoding($html, "UTF-8");
Do this before writing to the file in the first place.
I'm writing some code that can read in from a .txt file a display it on a webpage.
I had problems in my initial code, in that it would read in any text and it would erase whatever was in the document.
My original code:
function readIn(){
$input = fopen("input.txt", "r"); //Open the file, save opened file in input
$line = fgets($input);
fclose($input);
return $line
}
It only started working once I put in a While loop to go through EVERY LINE
function readIn(){
$input = fopen("input.txt", "r"); //Open the file, save opened file in input
$fullText = ""; //Variable full text
while(!feof($input)){
$line = fgets($input);
$fullText = $fullText . $line;
}
fclose($input);
return $fullText;
}
echo readIn();
Use "file_get_contents" to read an entire file into a variable, and then output in whatever fashion you choose.