Page 1 of 1

Chat room woes

Posted: Mon May 07, 2007 3:14 am
by ChessclubFriend
Ok this is part of a chatroom that I've made, my most important problem I'm having is when someone uses an apostrophe, that is, an, " ' " in their sentences. I'm not using a database right now so their chat is saved in chatFile.txt. stringData is just what is going to be saved to chatfile as someone's name and their chat, and it's pre-formated a little bit to look right once it hits the room. Whenever someone uses an apostrophe it enters a \ mark right before it. Does anyone know what I can do about this? Thanks

Code: Select all

$myFile = "chatFile.txt";
$whitespaceS = " shouts: ";

$fh = fopen($myFile, 'a') or die("can't open file");

$stringData = "$displayname$whitespaceS $ShoutInput $whitespaces";
fwrite($fh, $stringData);
fclose($fh);
This is how I display the chat...it's a second html page that's inside an IFrame on my first page, that reloads every 60 seconds, hoping that someone entered more chat into the text file that is the only displayed thing on the second page...:)

Code: Select all

<?php
$file = fopen("chatFile.txt","r");
$savedfile = fopen("chatFile.txt", "r");
while(! feof($file))
  {
  echo fgets($file). "<br />";
  }
fclose($file);
?>
Right now I'm using:
and lets assume stringdata = Hi my name's bob
the output on my chatserver is, Hi my name\'s bob

Code: Select all

if($stringdata)
  	{
	$string = $stringdata;
	$pattern = "/\'/";
	$replacement = "'";
	$stringdata = preg_replace($pattern, $replacement, $string);
	}
but this doesn't seem to find \'
the problem is when someone's chat contains an ' it replaces it with \' so I'm trying to search the string for \' before its wrote to file and replace it with ' like it should be, what is the correct pattern?? I know this sounds newbie but i can't find a list of pattern modifiers, I'm forced to ask you.

Posted: Mon May 07, 2007 1:42 pm
by RobertGonzalez

Posted: Mon May 07, 2007 2:14 pm
by John Cartwright
or turn off magic quotes :wink: