str_replace [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

str_replace [SOLVED]

Post 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?
Last edited by s.dot on Thu Aug 18, 2005 3:35 pm, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Fractal
Forum Commoner
Posts: 54
Joined: Tue Aug 16, 2005 1:28 pm

Post by Fractal »

$Text = preg_replace("(\"\(.+?)\"\/")is",'$1',$Text);

That's what I have.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

yeah... I'm no good with regexs :P

I would prefer to use str_replace();
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Fractal
Forum Commoner
Posts: 54
Joined: Tue Aug 16, 2005 1:28 pm

Post by Fractal »

scrotaye wrote:yeah... I'm no good with regexs :P

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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

With the GNU text utilities:

Code: Select all

sed 's/"//g' file.txt > new.file.txt
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

[script corrected]

Post 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
		}
	}
}
Last edited by raghavan20 on Thu Aug 18, 2005 8:03 am, edited 2 times in total.
korto
Forum Commoner
Posts: 36
Joined: Thu Aug 18, 2005 6:30 am
Location: Greece
Contact:

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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 " :(
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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 />";
korto
Forum Commoner
Posts: 36
Joined: Thu Aug 18, 2005 6:30 am
Location: Greece
Contact:

Post 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
Last edited by korto on Thu Aug 18, 2005 3:35 pm, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

haha wow, thanks. Another stupid mistake :(
All works good now.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply