Reputation: 48443
I need to process a big TXT file which contains notes from the orders.
Some notes look like Note for an order
, other ones like "Note for an order"
. I would need to remove the "
char from the respective string, if is in the begining and in the end of string.
Because the file is pretty large (±10MB), what is the fastest way to do it? What would you recommend me?
Upvotes: 0
Views: 4197
Reputation: 1976
You could read one line at a time, remove the " , and save file.
Reading one line at a time will be a lot less memory-intensive as compared to loading all the file at once. So, you can process any size of .txt
files, limited only by your computer speed.
Note: I assume that you are using Local Web Server
. Any shared hosting may not allow you to over-ride the 30 second max execution time limit. I recommend using this code on local XAMPP server.
<?php
set_time_limit(0); //To remove the max-execution time limit
$file_from = "foo.txt"; //File containing all the text
$file_to = "bar.txt"; //New File containing all the modified text
//open both files
$fp_source = fopen($file_from, "r") or die("Couldn't open ".$file_from);
$fp_dest = fopen($file_to, 'a+') or die("Couldn't open ".$file_to);
while (!feof($fp)) { //Continue loading domains till the end of file
$line = fgets($fp, 1024); //load one line at a time
$line = trim($line, '"'); // remove the first & last "
fwrite($file_to, $line); // save text in new file.
}
fclose($file_from); //close the handles
fclose($file_to); //close the handles
?>
The above code will replace all the " appearing at the first & last position in a string, where each string is differentiated from another by a new-line character.
If the " appear in the middle of the string, then you could replace this line
$line = trim($line, '"'); // remove the first & last "
with:
$line = str_replace('"', "", $line); // remove all the "
Although you have tagged the question as PHP
, but if it is only one file, or few files, I would recommend using the search & replace functions of any desktop text editor, like Notepad++ etc..
Upvotes: 2
Reputation: 47956
Processing a larger file like the one you mention will be an intensive process - I don't think there will be many ways you could optimize it.. Perhaps splitting the file into several chunks might help...
In any case, all you have to do is iterate over the file and for each line use the trim()
function. Here is a related post dealing with splitting a string by line breaks -
How to put string in array, split by new line?
Normally (by default) the trim()
function will strip whitespace characters from the beginning and end of a string, but you can specify any character to be trimmed...
trim — Strip whitespace (or other characters) from the beginning and end of a string.
Here is a simple example to remove the double quote character from the beginning and end of a string. -
$str = '"Hello "Stack" Overflow"';
$strippedStr = trim($str,'"');
echo $strippedStr;
// OUTPUT -> Hello "Stack" Overflow
Note that only the surrounding quotes were removed leaving the word stack
still wrapped with quotes.
Once you have removed the characters for each line, simply use the implode()
function to rejoin the lines.
Upvotes: 1
Reputation: 33504
Check each line as you get it from the file. I assume you are reading it in line by line - or if you are reading it in chunks (or the whole file) you are processing it line by line. In that case when you get the line and pop it into a $var
you can do something like this with trim():
$var=trim($yourLineOrColumn, '"');
and then deal with $var
instead.
Upvotes: 3