fwrite adds slashes to " "

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
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

fwrite adds slashes to " "

Post by nickman013 »

Hello,

I have a fwrite script that adds / (slashes) to my written data if there is a " so if somebody writes:

Nicky said "yo yo yo" it will out put Nicky Said "/ yo yo yo"/

Is there a way to stop that?

Thank You.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Post your code...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

stripslashes()
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

I dont have my code because I am not home right now.

Jshpro, where would I add that stripslashes() to?

If you need my code I will post soon. It is just a basic fwrite script.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Add it around the variable you want to strip the slashes from


ex 1.

Code: Select all

echo stripslashes($var)
ex 2.

Code: Select all

fwrite($handle, stripslashes($var))
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

jshpro2 wrote:stripslashes()
That doesn't strip forward slashes only back slashes.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Well the text he pasted does contain foward slashes:

"/ yo yo yo"/

however nothing standard in PHP will produce that effect, the closest match for the symptom he is experiencing is magic_quotes, which leads me to beleive the string he posted is an example and he probably meant \" yo yo yo \"


He had already been asked to show his code I was giving him something to try to see if we can skip that step, if it didn't work then its no big deal, just following you through my logic here
Last edited by josh on Thu Feb 02, 2006 1:19 am, edited 1 time in total.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

My guess is he used the wrong slash when describing the problem... if that isn't the case, then creating a function to replace or remove the slashes is best. It could utilize either str_replace(), preg_replace(), or as jshpro2 mentioned, stripslashes(), but if you're going to use stripslashes(), you'd need to use str_replace() to make it able to work, but at that point, the need to do the extra step is unnecessary...doing it just with str_replace() is fine in that case.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Here is my script.

Code: Select all

$content = "
<tr><td><b>$name</b> voted this muot as a $vote !<br><b>$name said $comments</td></tr>
";
$filename = '/home/muot/public_html/pages/voting/comments.txt'; 
if (is_writable($filename)) { 
if (!$handle = fopen($filename, 'a')) { 
echo "Cannot open file ($filename)"; 
exit; 
} 
if (fwrite($handle, $content) === FALSE) { 
echo "Cannot write to file ($somecontent)"; 
exit; 
} 
echo ""; 
fclose($handle); 
} else { 
echo "The file $filename is not writable"; 
} 
}
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

put this at the top of the script to see magic quotes is enabled..

Code: Select all

echo get_magic_quotes_gpc();
if enabled, you have to use strip slashes using stripslashes() and provide backslashes using mysql_real_escape_string()
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Can you clarify if it was forward or backward slashes?
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Frank Rafner voted this muot as a 4 !
Frank Rafner said Omg this is a enjoyable muot. When I am 80 years old the words of greg will echo through my head \"Oh my God frank him self\"
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

nickman013 wrote:
Frank Rafner voted this muot as a 4 !
Frank Rafner said Omg this is a enjoyable muot. When I am 80 years old the words of greg will echo through my head "Oh my God frank him self"
i hope this is POST or GET data...is not it?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

nickman013 wrote:
Frank Rafner voted this muot as a 4 !
Frank Rafner said Omg this is a enjoyable muot. When I am 80 years old the words of greg will echo through my head "Oh my God frank him self"
Ah, so I was right.. :)

stripslashes() is your friend.
Post Reply