Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : String Functions : stripslashes

stripslashes

Un-quote string quoted with addslashes (PHP 4, PHP 5)
string stripslashes ( string str )

Example 2454. A stripslashes() example

<?php
$str
= "Is your name O\'reilly?";

// Outputs: Is your name O'reilly?
echo stripslashes($str);
?>

Example 2455. Using stripslashes() on an array

<?php
function stripslashes_deep($value)
{
   
$value = is_array($value) ?
               
array_map('stripslashes_deep', $value) :
               
stripslashes($value);

   return
$value;
}

// Example
$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));
$array = stripslashes_deep($array);

// Output
print_r($array);
?>

The above example will output:

Array
(
   [0] => f'oo
   [1] => b'ar
   [2] => Array
       (
           [0] => fo'o
           [1] => b'ar
       )

)

Related Examples ( Source code ) » stripslashes




Code Examples / Notes » stripslashes

jab creations

When writing to a flatfile such as an HTML page you'll notice slashes being inserted. When you write to that page it's interesting how to apply stripslashes...
I replaced this line...
<?php fwrite($file, $_POST['textarea']); ?>
With...
<?php if (get_magic_quotes_gpc()) {fwrite ($file, stripslashes($_POST['textarea']));}?>
You have to directly apply stripslashes to $_POST, $_GET, $_REQUEST, and $_COOKIE.


alf

Take care using stripslashes() if the text you want to insert in the database contain \n characters ! You'll see "n" instead of (not seeing) "\n".
It should be no problem for XML, but is still boring ...


kibby

Okay, if using stripslashes_deep, it will definitely replace any NULL to "".  This will affect to coding that depends isset().  Please provide a workaround based on recent note.

r_loebs

Of course why not just do an
if($r){ stuff; } <-- this will check it all, NULL, 0, ""


hash

Might I warn readers that they should be vary careful with the use of stripslashes on Japanese text. The shift_jis character set includes a number of two-byte code charcters that contain the hex-value 0x5c (backslash) which will get stripped by this function thus garbling those characters.
What a nightmare!


mattyblah

It should be of note that if you are stripping slashes to get rid of the slashes added by magic_quotes_gpc then it will also remove slashes from \. This may not seem that bad but if you have someone enter text such as 'testing\' with a slash at the end, this will cause an error if not corrected. It's best to strip the slashes, then add a slash to every single slash using $text = str_replace('\\', '\\\\', $text);

allen

In response to Tim's solution, it is only good for one-dimensional array.  If the variables happened to be multi-dimensional arrays, we still have to use function like 'stripslashes_deep'.

stoic

in response to crab dot crab at gmail dot com:
$value need not be passed by reference. The 'stripped' value is returned. The passed value is not altered.


lukas.skowronski

If You want to delete all slashes from any table try to use my function:
function no_slashes($array)
{
foreach($array as $key=>$value)
{
if(is_array($value))
{
$value=no_slashes($value);
$array_temp[$key]=$value;
}
else
{
$array_temp[$key]=stripslashes($value);
}
}
return $array_temp;
}


10-feb-2005 03:45

If you want to deal with slashes in double-byte encodings, such as shift_jis or big5, you may use this:
<?
function stripslashes2($string) {
$string = str_replace("\\\"", "\"", $string);
$string = str_replace("\\'", "'", $string);
$string = str_replace("\\\\", "\\", $string);
return $string;
}
?>


dragonfly

If you are having trouble with stripslashes() corrupting binary data, try using urlencode() and urldecode() instead.

gregory

Here is code I use to clean the results from a MySQL query using the stripslashes function.
I do it by passing the sql result and the sql columns to the function strip_slashes_mysql_results.  This way, my data is already clean by the time I want to use it.
function db_query($querystring, $array, $columns)
{
 if (!$this->connect_to_mysql())
  return 0;
$queryresult = mysql_query($querystring, $this->link)
   or die("Invalid query: " . mysql_error());

 if(mysql_num_rows($queryresult))
 {
     $columns = mysql_field_names ($queryresult);

     if($array)
 {
         while($row = mysql_fetch_row($queryresult))
           $row_meta[] = $this->strip_slashes_mysql_results($row, $columns);
         return $row_meta;
     }
     else
     {
         while($row = mysql_fetch_object($queryresult))
           $row_meta[] = $this->strip_slashes_mysql_results($row, $columns);
         return $row_meta;
     }
 }
 else
return 0;
}

function strip_slashes_mysql_results($result, $columns)
{
foreach($columns as $column)
{
  if($this->debug)
  printp(sprintf("strip_slashes_mysql_results: %s",strip_slashes_mysql_results));
  $result->$column = stripslashes($result->$column);
}
return $result;
}


hauser dot j

Don't use stripslashes if you depend on the values NULL.
Apparently stripslashes converts NULL to string(0) ""
<?php
$a = null;
var_dump($a);
$b = stripslashes($a);
var_dump($b);
?>
Will output
NULL
string(0) ""


Change Language


Follow Navioo On Twitter
addcslashes
addslashes
bin2hex
chop
chr
chunk_split
convert_cyr_string
convert_uudecode
convert_uuencode
count_chars
crc32
crypt
echo
explode
fprintf
get_html_translation_table
hebrev
hebrevc
html_entity_decode
htmlentities
htmlspecialchars_decode
htmlspecialchars
implode
join
levenshtein
localeconv
ltrim
md5_file
md5
metaphone
money_format
nl_langinfo
nl2br
number_format
ord
parse_str
print
printf
quoted_printable_decode
quotemeta
rtrim
setlocale
sha1_file
sha1
similar_text
soundex
sprintf
sscanf
str_getcsv
str_ireplace
str_pad
str_repeat
str_replace
str_rot13
str_shuffle
str_split
str_word_count
strcasecmp
strchr
strcmp
strcoll
strcspn
strip_tags
stripcslashes
stripos
stripslashes
stristr
strlen
strnatcasecmp
strnatcmp
strncasecmp
strncmp
strpbrk
strpos
strrchr
strrev
strripos
strrpos
strspn
strstr
strtok
strtolower
strtoupper
strtr
substr_compare
substr_count
substr_replace
substr
trim
ucfirst
ucwords
vfprintf
vprintf
vsprintf
wordwrap
eXTReMe Tracker