Vote Up 0 Vote Down

stripslashes recursive php

How to stripslashes recursive deeply into an array in php like the following array :

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

Answers


Vote Up 0 Vote Down
Use the following function to strip the slashes in a php array recursively. It will be helpful when fetching and displaying a string data from mysql db sanitized by the mysql_real_escape_string() function.

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

return $value;
}

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

// Output
print_r($array);
// Print like $array = array("f'oo", "b'ar", array("fo'o", "b'ar"));
?>
flag | link |

Your Answer

Login before answering

Login with facebook