Chat room woes

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
ChessclubFriend
Forum Newbie
Posts: 21
Joined: Sun Mar 25, 2007 9:13 pm

Chat room woes

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

or turn off magic quotes :wink:
Post Reply