Page 1 of 1
str_replace [SOLVED]
Posted: Thu Aug 18, 2005 3:02 am
by s.dot
I need to remove all the double quotes from a txt file (I'd just use a text editor but the file is several megabytes long and tends to freeze)
I've tried several things, but none of them seem to work
I've tried:
Code: Select all
str_replace("\"","",$string); // nope
str_replace('"','',$string); // nope
str_replace(""","",$string); // nope
so how is it done?
Posted: Thu Aug 18, 2005 3:19 am
by Fractal
$Text = preg_replace("(\"\(.+?)\"\/")is",'$1',$Text);
That's what I have.
Posted: Thu Aug 18, 2005 3:37 am
by s.dot
yeah... I'm no good with regexs
I would prefer to use str_replace();
Posted: Thu Aug 18, 2005 3:40 am
by Fractal
scrotaye wrote:yeah... I'm no good with regexs
I would prefer to use str_replace();
Heh.. I'm not exactly sure how to do it with str_replace.. Hmmm
$Text = str_replace(""", " ", $Text);
I guess you could try that.
Posted: Thu Aug 18, 2005 4:59 am
by shiznatix
wait you just want to remove all occurences of " from a string right? well the one str_replace you have would do it.
Code: Select all
$string = str_replace('"', '', $string);
that will work and if it does not then there is somthing else wrong
Posted: Thu Aug 18, 2005 6:25 am
by timvw
With the GNU text utilities:
Code: Select all
sed 's/"//g' file.txt > new.file.txt
[script corrected]
Posted: Thu Aug 18, 2005 6:30 am
by raghavan20
I have not really tested this to work....but the logic seems ok...
Code: Select all
$fp = fopen('somefile.txt');
$fileSize = filesize($filename); //get the size of the file
$bytesRead = 0; //initially no bytes read
$mainData; //used to store the whole file
// read some data
if ($fileSize < 4096){
$maindata = fgets($fp, $fileSize);
$bytesRead = $fileSize; //not really necessary
}else{
$iteration = intval($fileSize/4096);
$pointerLocation = 0;
for ($i = 0; $i < $iteration + 1 ; $i++){//iteration + 1, for some bytes left not equal to 4096
fseek($fp, $bytesRead);
if ($fileSize - $bytesRead > 4096){
$mainData .= str_replace('"',' ', fgets($fp, 4096));//replace and append to $mainData
$bytesRead .= 4096;
}else{
$mainData .= str_replace('"', ' ', fgets($fp, ($fileSize - $bytesRead)));//replace and append to $mainData
$bytesRead .= $fileSize - $bytesRead;//not really necessary
}
}
}
Posted: Thu Aug 18, 2005 7:50 am
by korto
This has to work if you pass the file in a string variable ($string) (one solution to that is posted by raghavan20)
$newstring = str_replace('"','',$string);
in raghavan20's script the str_replace("\"\", " ", ... should I think be str_replace('"', ' ',...
as it was also advised by shiznatix
Posted: Thu Aug 18, 2005 2:13 pm
by s.dot
Code:
Code: Select all
$handle = fopen("ZIP_CODES.txt", "r");
while (!feof($handle))
{
$buffer = fgets($handle, 4096);
$buffer2 = str_replace('"','',$buffer);
$buffer3 = explode(",",$buffer);
echo $buffer3[0]."<BR>";
}
fclose($handle);
Result:
Code: Select all
"00501"
"00544"
"00601"
"00602"
"00603"
"00604"
"00605"
"00606"
"00610"
"00611"
"00612"
"00613"
"00614"
"00616"
"00617"
"00622"
"00623"
"00624"
"00627"
"00631"
etc...
Does not seem to be replacing the "

Posted: Thu Aug 18, 2005 2:56 pm
by raghavan20
this works!!! i hv tried it
Code: Select all
$str = "\"something\"";
echo "<br />"."replaced string:".str_replace("\"", "", $str)."<br />";
(or)
Code: Select all
$mainStr = "\"p\"h\"p\"d\"n\"";
echo "<br />"."preg_replace-replaced string:".preg_replace("/\"/", "", $mainStr)."<br />";
Posted: Thu Aug 18, 2005 3:32 pm
by korto
$buffer = fgets($handle, 4096);
$buffer2 = str_replace('"','',$buffer);
$buffer3 = explode(",",$buffer); -> you are not using the $buffer2 variable that holds the replaced data!
echo $buffer3[0]."<BR>";
You are putting the replaced buffer in $buffer2 and you are not using that variable everagain instead you are exploding the initial $buffer variable
As for ravaghan20 I am sure it works but for a string that has the format of the example you are giving ("\"string\") perhaps not for a string read from a file and has the "string" format
Posted: Thu Aug 18, 2005 3:34 pm
by s.dot
haha wow, thanks. Another stupid mistake

All works good now.