Question:
How do I remove the "\" before the special characters when writing files in PHP?
Rockstar
2008-12-06 10:56:48 UTC
I have created a function for writing HTML files, but when the file is written, it replaces all the escape characters such as ', #, $, with \', \#, and \$. How can I get around that? Does that have something to do with "Magic Quote"? If so, I'm not writing mySQL or anything, so it doesn't need to worry about that.
Three answers:
Brian K
2008-12-06 15:18:49 UTC
Unfortunately, it is almost certainly a Magic Quotes problem. It was a bad idea, and fortunately, it's scheduled to disappear in PHP 6.



If you can't turn it off in your php.ini file (or you need to be portable to installations not under your control), then you have to use stripslashes(), *but conditionally*.



You need to check get_magic_quotes_gpc() if your data comes from an HTTP request, or get_magic_quotes_runtime() if your data comes from a file.



If you use stripslashes unconditionally, then you remove too much at installations where magic quotes are turned off.
Kashyap Patel
2008-12-06 11:33:09 UTC
use addslashes(string) to add slash;



use find and replace to remove manually



like

replace \# with #
Cazpa
2008-12-06 11:04:48 UTC
try using stripslashes you can find more about it here:



http://uk3.php.net/stripslashes


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...