Struggling with backslashes ( 5 Views )
-
Hi all.
My problem is the following:
I have a little script that saves the content of a textarea on my site to a simple txt file. Discussion on that script can be found here: http://www.sitepoint.com/forums/showthread.php?t=162729
When I enter a ' or " in the text which is saved to the txt file, it automatically saves a backslash in front of it. If it would do that once, I would not mind. But every time the script is ran, another backslash is added.
Is there a way to go around this? I know the backslash is put there so that the PHP script is run properly without using the ' or " in the code. Just do not want it there...
Thanks
(HASAN, Ukraine)
$text = str_replace("\\","",$text);
Maybe this will help...? (you need the two slashes, otherwise php will "think" the first part of str_replace hasn't been closed... (or something like that)
(ömer, Niue)
I get an error now:
Notice: Undefined variable: text in write.php on line 16
write.php :
PHP Code:
if (isset($_POST['text'])) {
$f = fopen('bestel-kantoor.txt', 'w');
fwrite($f, $_POST['text']);
$text = str_replace("\\","",$text);
fclose($f);
}
(ayşee, Ghana)
PHP Code:
if (isset($_POST['text'])) {
$text = str_replace("\\","",$_POST['text']);
$f = fopen('bestel-kantoor.txt', 'w');
fwrite($f, $text);
fclose($f);
}
:)
(bülent, Georgia)
Doh...
Thanks.
(abaza_aysu, Madagascar)
If magic_quotes_gpc is set, PHP automatically escapes quotes, backslashes and so on. Try this:
PHP Code:
$text = get_magic_quotes_gpc() ? $_POST['text'] : addslashes($_POST['text']);
Edit: A little late ... :)
-- lilleman
(angelina, Yemen)
Here is a little function that is either just like, or somewhat like a function in Mojavi (php framework):
PHP Code:
function myStripSlashes (& $params) {
$keys = array_keys($params);
$count = sizeof($keys);
for ($i = 0; $i < $count; $i++) {
$value =& $params[$keys[$i]];
if (is_array($value)) {
myStripSlashes($value);
} else {
$value = stripslashes($value);
}
}
}
And to use it:
PHP Code:
if (ini_get('magic_quotes_gpc') == 1 ) {
if( sizeof($_GET) > 0 )
myStripSlashes($_GET);
if( sizeof($_POST) > 0 )
myStripSlashes($_POST);
if( sizeof($_COOKIE) > 0 )
myStripSlashes($_COOKIE);
}
If you just use that before all of your scripts, things should be OK.
Matt
(hüseyin, Palau)
Related Topics ... (or search in 1.720.883 topics !)
alternatives to backslashes (5) escaping backslashes (2) remove backslashes (5) backslashes in mysql? (5) help me with backslashes in php (7) double backslashes (2) backslashes on apostrophes? (6) fwrite giving me backslashes (2) backslashes preceeding content (8)
copyright © 2007-2031 Pfodere.COM ( 3 Pfoyihuee Online )
|