Jump to content

removing slashes from arrays


amedhussaini

Recommended Posts

Hey all,

 

Well, I'm starting to write some of my 'display_results.php' and i'm happy to see that slashes are in fact being added in ("hi i\'m here yay").    obviously i now want to get rid of em before i spit em out to be seen.  is there already an algorithm/function out there that will remove_slashes() for an entire array?

 

eg.

 

$result = mysqli_query($mysqli, $random_query);

$result_in_array = mysqli_fetch_assoc($result);

 

remove_slashes($result_in_array); ?

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/102070-removing-slashes-from-arrays/
Share on other sites

Hey all,

 

Well, I'm starting to write some of my 'display_results.php' and i'm happy to see that slashes are in fact being added in ("hi i\'m here yay").    obviously i now want to get rid of em before i spit em out to be seen.  is there already an algorithm/function out there that will remove_slashes() for an entire array?

 

eg.

 

$result = mysqli_query($mysqli, $random_query);

$result_in_array = mysqli_fetch_assoc($result);

 

remove_slashes($result_in_array); ?

 

Thanks in advance!

 

Use stripslashes($array) on anything you want to output that has slashes.  Like:

<?php
echo stripslashes($result_in_array['comment']);
?>

For example. =P

<?php
$a=array("member/","joh/n","redarr/ow");

foreach($a as $res){

$result=str_replace("/","",$res);

echo "$result <br>";

}


?>

 

Wrong slash.  He needs to remove \ from escaping chars.  He can just use stripslashes().  Or this:

<?php
function striparrayslash($array) {
foreach ($array as $k=>$v) {
  $array[$k] = stripslashes($v);
}
return $array
}
//usage:
$newarray = striparrayslash($oldarray);
print_r($newarray);
?>

Should work.  Tell me if there are any errors and I'll fix them.

Just tried it on my server and found a tiny error.  This function works 100%. =)

<?php

function striparrayslash($array) {
foreach ($array as $k=>$v) {
  $array[$k] = stripslashes($v);
}
return $array;
}
//usage:
$oldarray = array("test"=>"lo\\l"); //I escaped the backslash so it displays in the array. =)
$newarray = striparrayslash($oldarray);
print_r($newarray);
?>

Just another approach for fun :)

 

<?php
function stripthoseslashes(&$input){
     stripslashes($input);
}
$arr = array("me\\","you\\","hi\\");
array_walk($arr, 'stripthoseslashes');
print_r($arr);?>

 

You could do that.  I don't like array_walk() for whatever reason, though. :o 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.